Egos Zhang
Egos Zhang

Reputation: 1364

Why charles can not capture http request by c++?

I write http request code by c++ and Java. But Charles can only capture http request by Java. Why ? Charles can't get any http request? Charles can't capture http request by c++?

c++ main code:

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 main code:

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()));

Both c++ and Java do the same thing.I can capture http request by Wireshark. I try many times and use many different methods, but it also doesn't work.Can you tell me why?

Upvotes: 5

Views: 392

Answers (1)

Ajay
Ajay

Reputation: 18431

You probably need to open this way:

socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

And need to use \r\n instead of just \n everywhere.

Try with GET verb first. The HTTP request message is long and complex, first test with simple message.

Code:

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

Upvotes: 1

Related Questions