高手帮忙分析http代理连接问题!

dingzhongfeng 2006-01-19 08:52:25
与代理服务器连接已建立,但是在发送CONNCET请求令到代理服务器时,总出现400 Bad Request (请求出现语法错误),请高手帮忙分析
请求部分代码如下:
strProxyAddr = _T("192.168.0.7");
nPort = 80;

CString strRequest;
char szBuf[1024];
strRequest.Format("CONNECT %s:%d HTTP/1.1\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32)\r\n\r\n", strProxyAddr, nPort);
strcpy(szBuf, strRequest);

SendRequest(...);//请求成功
GetHttpHeader(...);//返回HTTP/1.0 400...



...全文
187 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
醉马不肖 2006-01-23
  • 打赏
  • 举报
回复
host需要有
etre 2006-01-23
  • 打赏
  • 举报
回复
400是语法错误
("CONNECT %s:%d HTTP/1.0\r\n",lpszHost,nPort);
("%s: %s\r\n","Host",lpszHost);
("%s: %s\r\n","Connection","Keep-Alive");
("%s: %s\r\n","Proxy-Connection","Keep-Alive");
("Content-Length: %d\r\n\r\n",0);
上面的写法是可行的.经过很多代理验证的
dingzhongfeng 2006-01-23
  • 打赏
  • 举报
回复
是m_s的问题,以解决,感谢etre(林荃)
dingzhongfeng 2006-01-23
  • 打赏
  • 举报
回复
还是连不上啊

如etre 所说,我试了
代码如下:
可还是返回
{"HTTP/1.0 400 Malformed Request
Pragma: no-cache
Content-type: text/html

"}

发送前


BOOL CHttpSocket::ConnectByProxy(LPCTSTR lpszHost, UINT nPort)
{
struct sockaddr_in SocketAddr; // address socket

m_s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
SocketAddr.sin_addr.s_addr = inet_addr(lpszHost);
SocketAddr.sin_family=AF_INET;
SocketAddr.sin_port=htons(nPort);

struct fd_set fdSet;
struct timeval tmvTimeout={0L, 0L};

FD_ZERO(&fdSet);
FD_SET(m_s, &fdSet);

if (select(0,&fdSet,NULL,NULL,&tmvTimeout)==SOCKET_ERROR)
{
closesocket(m_s);
AfxMessageBox("Error when select.");
return 0;
}

if (connect(m_s, (const struct sockaddr *)&SocketAddr, sizeof(SocketAddr))==SOCKET_ERROR)
{
return 0;
}

CString strTmp1,strTmp2,strTmp3,strTmp4,strTmp5;
strTmp1.Format("CONNECT %s:%d HTTP/1.0\r\n",lpszHost,nPort);
strTmp2.Format("%s: %s\r\n","Host", lpszHost);
strTmp3.Format("%s: %s\r\n","Connection","Keep-Alive");
strTmp4.Format("%s: %s\r\n","Proxy-Connection","Keep-Alive");
strTmp5.Format("Content-Length: %d\r\n\r\n",0);

CString strSend = strTmp1+strTmp2+strTmp3+strTmp4+strTmp5;
//strSend的值为
/*{"CONNECT 192.168.0.7:80 HTTP/1.0
Host: 192.168.0.7
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
Content-Length: 0

"}*/

if(!SocketSend(m_s, strSend))
{
return 0;
}

//以上成功,运行下面GetHttpHeader(...)后strRecv 的值为
/*{"HTTP/1.0 400 Malformed Request
Pragma: no-cache
Content-type: text/html

"}*/

char szHttpBuffer[1024];
ZeroMemory(szHttpBuffer, 1024);
GetHttpHeader(m_s, szHttpBuffer);
CString strRecv = szHttpBuffer;
if(strRecv.Find("HTTP/1.0 200 Connection established",0)==-1)
{
return 0;
}

return 1;
}
BOYGUARD110 2006-01-22
  • 打赏
  • 举报
回复
mark
dingzhongfeng 2006-01-20
  • 打赏
  • 举报
回复
我是参照这篇文章做的:
http://blog.csdn.net/mb459/archive/2005/08/19/458745.aspx

请高手帮忙分析一下呀,谢谢!
dingzhongfeng 2006-01-20
  • 打赏
  • 举报
回复
编程环境QQ、FlashGet、netants都能通过http代理连接或下载。
dingzhongfeng 2006-01-20
  • 打赏
  • 举报
回复
我要达到的目的是以http代理方式去连接远程服务器。现在问题出现在无法与局域网代理服务器建立连接,即给连接公网的代理服务器发送的CONNECT请求返回的却是“HTTP/1.0 400...”
aa3000 2006-01-20
  • 打赏
  • 举报
回复
你试试用 netants 下载相同的页面,看看里面的 log 是否跟你的一样。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧