我按http request
和c++
编写Java
代码。但Charles
只能通过http request
捕获Java
。为什么? Charles
无法获得http request
? Charles
无法通过http request
抓取c++
?
c ++主要代码:
int sockfd, ret, i, h;
struct sockaddr_in servaddr;
char str1[4096], str2[4096], buf[BUFSIZE], *str;
socklen_t len;
fd_set t_set1;
struct timeval tv;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
exit(0);
};
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ){
exit(0);
};
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
exit(0);
}
memset(str2, 0, 4096);
strcat(str2, "qqCode=474497857");
str=(char *)malloc(128);
len = strlen(str2);
sprintf(str, "%d", len);
memset(str1, 0, 4096);
strcat(str1, "POST /webservices/qqOnlineWebService.asmx/qqCheckOnline HTTP/1.1\n");
strcat(str1, "Host: www.webxml.com.cn\n");
strcat(str1, "Content-Type: application/x-www-form-urlencoded\n");
strcat(str1, "Content-Length: ");
strcat(str1, str);
strcat(str1, "\n\n");
strcat(str1, str2);
strcat(str1, "\r\n\r\n");
printf("%s\n",str1);
ret = write(sockfd,str1,strlen(str1));
Java主代码:
BufferedReader in = null;
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
c++
和Java
都做同样的事情。我可以通过Wireshark
捕获http请求。我尝试了很多次并使用了许多不同的方法,但它也没有用。你能告诉我为什么吗?
答案 0 :(得分:1)
您可能需要以这种方式打开:
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
并且需要在任何地方使用\r\n
而不仅仅是\n
。
首先尝试使用GET
动词。 HTTP请求消息冗长而复杂,首先使用简单消息进行测试。
<强>代码:强>
auto theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (!theSocket)
{
printf("Socket open failed");
return 1;
}
printf("\nSocket Opened.");
auto result = connect(theSocket, (SOCKADDR*)&clientService, sizeof(clientService));
if (result == -1)
{
printf("Connected failed");
return 2;
}
printf("\nConnected");
char buf[] = { "GET //releases.ubuntu.com/16.04/ubuntu-16.04-server-amd64.iso?_ga=1.24198131.837286503.1462436453 HTTP/1.0\r\n\r\n" };
auto r = send(theSocket, buf, strlen(buf), 0);