1、请求连接到代理上
///////////////////////////////////////////////
BOOL CAsyncSocket::Connect( LPCTSTR lpszHostAddress, UINT nHostPort )
{
    //转换目标http server的地址
    ASSERT(lpszHostAddress != NULL);    SOCKADDR_IN sockAddr;
    memset(&sockAddr,0,sizeof(sockAddr));    sockAddr.sin_family = AF_INET;
    sockAddr.sin_addr.s_addr = inet_addr(lpszHostAddress);

    if (sockAddr.sin_addr.s_addr == INADDR_NONE)
    {
LPHOSTENT lphost = gethostbyname(lpszHostAddress);
if (lphost != NULL)
{
          sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
}
else  
{
    BOOL bRes = CAsyncSocket::Connect(m_strProxyHost, m_nProxyPort);
    if (!bRes)
   {
if (GetLastError() != WSAEWOULDBLOCK)
{
          m_nProxyError = PROXYERROR_NOCONN;
     return FALSE;
}
    }
    m_nProxyPeerPort = htons((u_short)nHostPort);
    m_nProxyPeerIp   = 0;
    m_ProxyPeerHost  = lpszHostAddress;
    return TRUE;
}
    }
 
    sockAddr.sin_port = htons((u_short)nHostPort);
    BOOL bRes = Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
    if (bRes || GetLastError() == WSAEWOULDBLOCK)
    {
m_ProxyPeerHost = lpszHostAddress;
    }
return bRes;
}
2、OnConnect响应函数
void CHttpAsyncSocket::OnConnect(int nErrorCode) 
{
    CAsyncSocket::OnConnect(nErrorCode);
    if (nErrorCode)
    { //不能连接到代理上
Close();
return;
    }    //发送连接到目标server的请求
    CString str;

    str.Format("CONNECT %s:%d HTTP/1.0\r\nHost: %s:%d\r\n\r\n",
      m_ProxyPeerHost, ntohs(m_nProxyPeerPort), m_ProxyPeerHost, ntohs(m_nProxyPeerPort));    int num = CAsyncSocket::Send(str, str.GetLength());
    if (numsent == SOCKET_ERROR || numsent < str.GetLength())
    {
if (GetLastError()!=WSAEWOULDBLOCK || numsent<str.GetLength())
{
               Close();
      return;
}
     }
}
3、在OnReceive里面收到server发来的回复却是"HTTP/1.0 403 Forbidden",
但是我在IE里面设置却是有效地,能成功下载文件,不知道上面的流程哪个地方有问题?请各位大侠指点!不胜感激!

解决方案 »

  1.   

    "HTTP/1.0 403 Forbidden"是HTTP代理返回的?
    试试:
    str.Format("CONNECT %s:%d HTTP/1.1\r\nUser-Agent: EA/0.1\r\n\r\n", m_ProxyPeerHost, m_nProxyPeerPort);
      

  2.   

    我试过了,还是没用,下面是代理(218.90.139.186:3128)返回的完整的包,可能是说目标URL"sq2.onlinedown.net:80"地址达不到?
    HTTP/1.0 403 Forbidden
    Server: Squid/2.4.STABLE6
    Mime-Version: 1.0
    Date: Sat, 06 Sep 2003 04:11:12 GMT
    Content-Type: text/html
    Content-Length: 697
    Expires: Sat, 06 Sep 2003 04:11:12 GMT
    X-Squid-Error: ERR_ACCESS_DENIED 0
    X-Cache: MISS from ns.mydomain
    Proxy-Connection: keep-alive<HTML><HEAD>
    <TITLE>ERROR: The requested URL could not be retrieved</TITLE>
    </HEAD><BODY>
    <H1>ERROR</H1>
    <H2>The requested URL could not be retrieved</H2>
    <HR>
    <P>
    While trying to retrieve the URL:
    <A HREF="sq2.onlin down.net:80">sq2.onlinedown.net:80</A>
    <P>
    The following error was encountered:
    <UL>
    <LI>
    <STRONG>
    Access Denied.
    </STRONG>
    <P>
    Access control configuration prevents your request from
    being allowed at this time.  Please contact your service provider if
    you feel this is incorrect.
    </UL>
    <P>Your cache administrator is <A HREF="mailto:root">root</A>. 
    <br clear="all">
    <hr noshade size=1>
    Generated Sat, 06 Sep 2003 04:11:12 GMT by ns.mydomain (Squid/2.4.STABLE6)
    </BODY></HTML>
      

  3.   

    你看看IE发送了什么内容
    可能那个网站需要cookie
      

  4.   

    我的,我用sniff看看,但是我总觉的是自己的设置不对....或者请求方法缺什么似的:)
      

  5.   

    服务器真的启动了?那通过代理要连接的服务器端口对不对?我也用这个类写过http,sock5代理,没出现什么问题阿