为什么查尔斯无法通过c ++捕获http请求?

时间:2016-05-26 04:10:40

标签: java c++ wireshark charles-proxy

我按http requestc++编写Java代码。但Charles只能通过http request捕获Java。为什么? Charles无法获得http requestCharles无法通过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请求。我尝试了很多次并使用了许多不同的方法,但它也没有用。你能告诉我为什么吗?

1 个答案:

答案 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);