为啥请求回来的是乱码呢?codeblocks里面编译的

shiter
人工智能领域优质创作者
博客专家认证
2013-03-27 03:45:12

/*****************************************************************/
/*********** 程序名称:get_http.c ********************************/
/*********** 功能:向固定地址发起请求得到返回页面字符 ************/
/*********** 作者:YZW ******************************************/
/*********** 时间:2010-10-28 ************************************/
/*****************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>


int htconnect(char *host, int port){
int white_sock;
struct hostent * site;
struct sockaddr_in me;
site = gethostbyname(host);
if (site==NULL) return -2;
white_sock = socket(AF_INET,SOCK_STREAM,0);
if (white_sock <0) return -1;
memset(&me, 0, sizeof(struct sockaddr_in));
memcpy(&me.sin_addr, site-> h_addr_list[0], site-> h_length);
me.sin_family = AF_INET;
me.sin_port = htons(port);
return (connect(white_sock, (struct sockaddr *)&me,sizeof(struct sockaddr)) <0) ? -1 : white_sock;
}

int htsend(int sock, char *fmt, ...){
char BUF[1024];
va_list argptr;
va_start(argptr,fmt);
vsprintf(BUF,fmt,argptr);
va_end(argptr);
//printf(BUF); //回显
//printf("-->%s<--\n",BUF);
return send(sock,BUF,strlen(BUF),0);
}


int main(int argc,char **argv){
int black_sock;
int M,N;
char bugs_bunny[3];
// if (argc<2){
// printf( "Please input the hostName. Example:\n %s www.sohu.com\n ",argv[0]);
// return;
// }
char *host_name = "www.sohu.com";
black_sock = htconnect(host_name,80);
if (black_sock <1) return;

printf("循环发送请求开始\n");
printf("......\n");
M=1;N=1;
while (N==1){
if(M % 1000 == 0){printf("当前循环次数:%d \n",M); }
if(M == 10000){break;}
htsend(black_sock, "GET / HTTP/1.0\n");
htsend(black_sock, "Host: %s\n", host_name);
htsend(black_sock, "\n");
//htsend(black_sock, "%c ", 10);
M++;
}
printf("循环发送请求 %d 次\n",M);

printf("收到信息Start:---->\n");
while (read(black_sock, bugs_bunny, 1)> 0){
printf( "%c",bugs_bunny[0]);
}
printf( "收到信息End.<----\n ");
close(black_sock);
}

...全文
117 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiter 2013-03-29
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode:0x4A 0x55,UTF-8:0xE5 0x95 0x8A
有道理
赵4老师 2013-03-27
  • 打赏
  • 举报
回复
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode:0x4A 0x55,UTF-8:0xE5 0x95 0x8A
Red_angelX 2013-03-27
  • 打赏
  • 举报
回复
编码问题? 目标网站返回的是什么编码
shiter 2013-03-27
  • 打赏
  • 举报
回复
在环境设置里进行如下设置: 把Terminal to launch console programs那个选项改成gnome-terminal -t $TITLE -x 原来是xterm -T $TITLE -e

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧