我正在开发一个小项目(C编程)来隧道通过代理,当我尝试这样做时,我收到一个错误:
HTTP/1.0 400 Bad Request
Server: squid/2.7.STABLE9
Date: Mon, 07 Jan 2013 22:20:56 GMT
Content-Type: text/html
Content-Length: 1456
X-Squid-Error: ERR_INVALID_REQ 0
X-Cache: MISS from xt03
Via: 1.0 xt03:80 (squid/2.7.STABLE9)
Connection: close
我发送给代理服务器的数据是:
char msg[] = "CONNECT example.com:80 HTTP/1.0 <CR><LF> HOST example.com:80 <CR><LF> \n";
任何帮助都会很棒!
------更新------
我尝试了你们建议的方法,但我仍然遇到错误。我将发布我的整个代码集,看看它是否在套接字中导致它发送错误的信息。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define maxlen 2048
int main(int argc, char *argv[])
{
int mysocket;
int len;
char buffer[2000];
char msg[] = "CONNECT example.org:80 HTTP/1.0 \r\n HOST example.org:80 \r\n\r\n";
mysocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in dest;
memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr("125.39.66.150");
dest.sin_port = htons(80);
connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
send(mysocket, msg, strlen(msg), 0);
len = recv(mysocket, buffer, maxlen, 0);
buffer[len] = '\0';
printf("%s \n", buffer);
close(mysocket);
return 0;
}
-----更新------
新错误:
HTTP/1.0 400 Bad Request
Server: squid/2.7.STABLE5
Date: Tue, 08 Jan 2013 22:40:24 GMT
Content-Type: text/html
Content-Length: 1158
X-Squid-Error: ERR_INVALID_REQ 0
X-Cache: MISS from DXT-BJ-219
X-Cache-Lookup: NONE from DXT-BJ-219:80
Connection: close
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The requested URL could not be retrieved</TITLE>
<STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans- serif}PRE{font-family:sans-serif}--></STYLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The requested URL could not be retrieved</H2>
<HR noshade size="1px">
<P>
While trying to process the request:
<PRE>
CONNECT example.com:80 HTTP/1.1
</PRE>
<P>
The following error was encountered:
<UL>
<LI>
<STRONG>
Invalid Request
</STRONG>
</UL>
<P>
Some aspect of the HTTP Request is invalid. Possible problems:
<UL>
<LI>Missing or unknown request method
<LI>Missing URL
<LI>Missing HTTP Identifier (HTTP/1.0)
<LI>Request is too large
<LI>Content-Length missing for POST or PUT requests
<LI>Illegal character in hostname; underscores are not allowed
</UL>
<P>Your cache administrator is <A HREF="mailto:noc_admin@163.com">noc_admin@163.com</A>.
<BR clear="all">
<HR noshade size="1px">
<ADDRESS>
by DXT-BJ-219
</ADDRESS>
</BODY></HTML>
答案 0 :(得分:3)
“错误请求”表示您发送到服务器的请求格式错误且无法理解。我认为这样做的原因是你发送 8 字符<CR><LF>
(如','&lt;','C','R'等)而不是2个字符他们应该代表,即\r\n
。
尝试
char msg[] = "CONNECT example.com:80 HTTP/1.0\r\nHOST example:80\r\n\r\n";
答案 1 :(得分:0)
请参阅:rfc2616
10.4.1 400错误请求
由于语法格式错误,服务器无法理解该请求。客户端不应该在没有修改的情况下重复请求。
当你通过时:
char msg[] = "CONNECT example.com:80 HTTP/1.0 <CR><LF> HOST example.com:80 <CR><LF> \n";
然后,<CR>
和<LF>
部分可能会以字符串形式发送,而不是ASCII 0xA和0xD,它们代表您要发送的控制字符。
最简单的解决方法是使用C等价物:\r
<CR>
和\n
<LF>
:
char msg[] = "CONNECT example.com:80 HTTP/1.0 \r\n HOST example.com:80 \r\n\r\n";
如果您打算扩展msg[]
数组的使用以包含更多请求,则可能必须编写包装函数以将msg[]
中的CR,LF和其他控制字符串转换为适当的C char等同于形成正确的信息。
希望这有帮助。