#include <stdio.h>
#include <windows.h>
#include "Wininet.h"
#pragma comment(lib,"Wininet.lib")void main()
{
HINTERNET hRoot = InternetOpen("user", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
HINTERNET hConn = InternetConnect( hRoot, TEXT("192.168.0.168/checkuserid.asp"),
INTERNET_DEFAULT_HTTP_PORT, 
NULL, NULL,
INTERNET_SERVICE_HTTP,
0, 0 );
HINTERNET hRequest = HttpOpenRequest( hConn, TEXT("POST"), NULL, 
NULL, NULL, NULL, 0, 0); char data[32] = "userid=123456"; //这个是userid
BOOL b = HttpSendRequest( hRequest, NULL, 0, (LPVOID)data, strlen(data)); char buffer[40960]={0};
DWORD count;
FILE* file = fopen("e:\\1.html", "w+");
while(1)
{
InternetReadFile( hRequest, buffer, 40960, &count );
if(count==0)
break;
fwrite( buffer, 1, count, file );
}
fclose(file); InternetCloseHandle(hRequest);
InternetCloseHandle(hConn);
InternetCloseHandle(hRoot);
}/////////////////////////以上是程序,我要向这个网页checkuserid.asp,提交一个userid,网页收到后会把这个id放到数据库。
但最后返回的页面老是:
Bad Request
Your browser sent a request that this server could not understand.
而且ID也没有被放进数据库。但是,我把192.168.0.168/checkuserid.asp?userid=123456这个网址放进ie里执行,就可以了,id也被放到数据库了。我的程序有哪儿写得不对呢?

解决方案 »

  1.   

    http://support.microsoft.com/kb/165298
      

  2.   

    给你段我以前写的代码吧,很简单..BOOL GetHttpInfo(DWORD &dwCode)
    {
    CString sIP,sMationName,sTemp;
    GetCurrentIP(sIP,sMationName);
      sTemp.Format("http://www.111.com/vice.asp?clientname=%s&netbar=%s&ipaddr=%s&security=1&type=1&action=ad",sMationName,this->sNetbarID,sIP);

    CInternetSession *pSession = NULL;

    pSession = new CInternetSession;
    try
    {
    CHttpFile *pFile = (CHttpFile*)pSession->OpenURL(sTemp,1,INTERNET_FLAG_TRANSFER_BINARY);

    CString sState;

    pFile->QueryInfoStatusCode(dwCode);

    if (pFile)
    {
    CString strList;

    BOOL bResult = FALSE;
    for (int i=0;bResult = pFile->ReadString(strList);i++)
    {   
                                    //这里是处理数据,每次接受到一行数据,你可以打印出来
    DisposeAnswer(strList);
    }
    pFile->Close();
    delete pFile;
    delete pSession;
    return TRUE;
    }
    else
    {
    delete pSession;
    return FALSE;
    }
    }
    catch(CInternetException *m_pException)
    {
    m_pException->Delete();  
    return FALSE;
    }

    }
      

  3.   

    上面那个代码的DisposeAnswer 是我自己的函数,解析返回值的,
    或者lz也可以去用sniffer抓个包,改掉相应的字段并在content-length改成你的长度然后用tcp发到80端口就可以了
      

  4.   

    把HttpOpenRequest中的POST类型改成GET类型,或者使用SAFEARRAY<UI1> 类型的数据来HttpSendRequest。post数据时不能直接使用字符串,必须使用SAFEARRAY
      

  5.   

    改成这样:
    HINTERNET hRoot = InternetOpen("user", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); 
    HINTERNET hConn = InternetConnect( hRoot, TEXT("192.168.0.168"), 
    INTERNET_DEFAULT_HTTP_PORT,  
    NULL, NULL, 
    INTERNET_SERVICE_HTTP, 
    0, 0 ); 
    HINTERNET hRequest = HttpOpenRequest( hConn, TEXT("POST"), "/checkuserid.asp",  
    NULL, NULL, NULL, 0, 0); 
      

  6.   

    上面那个链接是微软给出的办法,也不行吗?代码是:
       static TCHAR hdrs[] =
          _T("Content-Type: application/x-www-form-urlencoded");
       static TCHAR frmdata[] =
          _T("name=John+Doe&userid=hithere&other=P%26Q");
      static LPSTR accept[2]={"*/*", NULL};   // for clarity, error-checking has been removed
       HINTERNET hSession = InternetOpen("MyAgent",
          INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
       HINTERNET hConnect = InternetConnect(hSession, _T("ServerNameHere"),
          INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
       HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
          _T("FormActionHere"), NULL, NULL, accept, 0, 1);
       HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
       // close any valid internet-handles贴出错误信息。
      

  7.   

    void main()
    {
    static TCHAR hdrs[] =
    TEXT("Content-Type: application/x-www-form-urlencoded");
    static TCHAR frmdata[] =
    TEXT("userid=1000354");
    static LPSTR accept[2]={"*/*", NULL};   // for clarity, error-checking has been removed
       HINTERNET hSession = InternetOpen("MyAgent",
          INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
       HINTERNET hConnect = InternetConnect(hSession, TEXT("192.168.0.168/checkimgfunction.asp"),
          INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
       HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
          TEXT("FormActionHere"), NULL, NULL, (LPCTSTR*)accept, 0, 1);
       BOOL b = HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
       // close any valid internet-handles

       char buffer[40960]={0};
    DWORD count;
    FILE* file = fopen("e:\\1.html", "w+");
    while(1)
    {
    InternetReadFile( hRequest, buffer, 40960, &count );
    if(count==0)
    break;
    fwrite( buffer, 1, count, file );
    }
    fclose(file);}我这么写的,最后得到1.html内容是
    Bad Request
    Your browser sent a request that this server could not understand.
      

  8.   

    改造一下:
    将上面那个"/checkimgfunction.asp"去掉,把后面的“FormActionHere”改成"/checkimgfunction.asp"