java socket recv 乱码_c++利用socket模拟http,返回乱码

我利用c++的socket模拟了http访问某网站,结果返回乱码。

这个乱码很奇怪,它不是完全乱码,具体情况就是:

一部分中文乱码,一部分英文乱码,大部分还是正确的。

我用wireshark查看的时候,发现http返回是200。同时,我查看了返回的html文本,也是正确的。但就是在用socket接收的时候,会出现部分乱码。

对于本程序,我的思路是:

在myhttp.h中,封装一个mysocket的类,实现socket的connect,send和recv。recv是采用非阻塞的方式。为了保证所有的来自服务端的数据已经在缓存区,所以在send后sleep(1)后才调用recv。另外,我还封装了一个myhttp类,继承于mysocket。myhttp主要是接收各种http参数,然后构造出一个http请求,然后发送。

test.cc主要是对myhttp的调用。

下面是我的代码:

myhttp.h

#include

#include

#include

#include

#include

#include

#include

#include

#include

class mysocket{

private:

int sockfd;

struct sockaddr_in servaddr;

string response;

protected:

mysocket(string ip,string port){

const char * c_ip = ip.c_str();

const char * c_port = port.c_str();

this->sockfd = socket(AF_INET,SOCK_STREAM,0);

memset(&(this->servaddr),0,sizeof(this->servaddr));

(this->servaddr).sin_family = AF_INET;

(this->servaddr).sin_port = htons(atoi(c_port));

inet_pton(AF_INET,c_ip,&(this->servaddr).sin_addr);

}

void mysocket_connect(){

connect(this->sockfd,(struct sockaddr*)&(this->servaddr),sizeof(this->servaddr));

}

void mysocket_send(string request){

const char * c_request = request.c_str();

send(this->sockfd,c_request,strlen(c_request),0);

sleep(1);

}

void mysocket_recv(){

char buf[1024];

memset(&buf,0,sizeof(buf));

while(int recvlength = recv(this->sockfd,buf,sizeof(buf),MSG_DONTWAIT)){

if(recvlength < 0)

break;

else{

this->response += buf;

memset(&buf,0,sizeof(buf));

}

}

cout << this->response;

}

};

class myhttp:public mysocket{

private:

string ip;

string port;

//得到数据length

string getLength(string data){

int length = data.size();

char lenstr[12] = {0};

sprintf(lenstr,"%d",length);

return string(lenstr);

}

public:

myhttp(string ip,string port):mysocket(ip,port){ //调用父类构造函数

this->ip = ip;

this->port = port;

}

//path,addition

void GET(string path,initializer_list args){

//处理可变长度参数

string addition = "";

for(auto arg : args)

addition = addition + arg + "\r\n";

//形成http请求

string http_request = "GET " + path + " HTTP/1.1\r\nHost: " + this->ip + ":" + this->port + "\r\n" + addition + "\r\n";

this->mysocket_connect();

this->mysocket_send(http_request);

this->mysocket_recv();

//while(1){}

}

//path,data,contentType,addition

void POST(string path,string data,string contentType,initializer_list args){

//处理可变长度参数

string addition = "";

for(auto arg : args)

addition = addition + arg + "\r\n";

//得到data长度

string length = this->getLength(data);

//形成http请求

string http_request = "POST " + path + " HTTP/1.1\r\nHost: " + this->ip + ":" + this->port + "\r\nContent-Type: " + contentType + "\r\ncontent-length: " + length + "\r\n" + addition + "\r\n" + data;

this->mysocket_connect();

this->mysocket_send(http_request);

this->mysocket_recv();

//while(1){}

}

};

test.cc

#include

#include

using namespace std;

#include "myhttp.h"

int main(){

myhttp * Myhttp = new myhttp("123.57.236.235","80");

Myhttp->GET("/Public/js/jquery.min.js",{});

return 0;

}

乱码部分(比如对某jquery文件的访问的返回的文本):

6d738d9a159006e95220ef74a9cd79f8.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值