我正在尝试在包含授权标头的c编程上实现http(GET请求),但问题是我无法获得(200 ok),我只有一个通信会话(服务器回复需要401授权)但不超过。
**while(1){
strcpy(buffer,"GET /mywebsite/login.html/ HTTP1.1 \n\n");
strcat(buffer,"Host: localhost\n\n");
strcat(buffer,"Connection: keep-alive\n\n");
strcat(buffer,"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11\n\n");
strcat(buffer,"Accept: text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8\n\n");
strcat(buffer,"Accept-Encoding: gzip,deflate,sdch\n\n");
strcat(buffer,"Accept-Language: en-US,en;q=0.8\n\n");
strcat(buffer,"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\n\n");
status = send(sock, buffer, strlen(buffer), 0);
if(status == -1) {
printf("Error in send(). Error code: %d\n", WSAGetLastError());
continue;
}
buffer[0]='\0';
//The receiving functions
status = recv(sock, buffer, BUFFER_SIZE, 0);
if (status == -1) {
printf("Error in recv(). Error code: %d\n", WSAGetLastError());
continue;
}
buffer[status] = '\0';
printf("The message received is: ");
printf("%s\n\n", buffer);
strcpy(buffer,"GET /mywebsite/login.html/ HTTP1.1\n\n");
strcat(buffer,"Host: localhost\n\n");
strcat(buffer,"Authorization: Basic QWRtaW46MTIzNA== \n\n");
strcat(buffer,"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11\n\n");
strcat(buffer,"Accept: text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8\n\n");
strcat(buffer,"Accept-Encoding: gzip,deflate,sdch\n\n");
strcat(buffer,"Accept-Language: en-US,en;q=0.8\n\n");
strcat(buffer,"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\n\n");
strcat(buffer,"Connection: keep-alive\n\n");
status = send(sock, buffer, strlen(buffer), 0);
if(status == -1) {
printf("Error in send(). Error code: %d\n", WSAGetLastError());
continue;
}
buffer[0]='\0';
//The receiving functions
status = recv(sock, buffer, BUFFER_SIZE, 0);
if (status == -1) {
printf("Error in recv(). Error code: %d\n", WSAGetLastError());
continue;
}
buffer[status] = '\0';
printf("%s\n\n", buffer);
buffer[0]='\0';
}**
第一个标头发送成功,但是服务器一直无所事事的下一个标头(没有回复) 请在这里帮助不大我不知道问题是什么...... :(
答案 0 :(得分:2)
有一些明显的问题:
send
和recv
== EINTR send
可以发送少于请求的字节数,因此应该在循环中使用它以确保传输所有数据。