现在本机用ASP配好了一个IIS,现在自己编写客户端,要求通过socket利用http协议与服务器端进行通信,现在的要求就是在自己编写的客户端输入一串字符串,使之能够上传到服务器,我想应该是用POST格式吧,在网上搜了相关资料,发现多数是关于GET的,也看了RFC2616,发现上面也没详细说明POST的格式,故特来请教。另外附上自己根据网上资料编的request,但是最后运行时提示:400 Bad Request
sprintf(request, "POST http://localhost:6000/default.asp HTTP/1.1\r\n\
Accept: */*\r\n\
                  User-Agent: MSIE 6.0\r\n\
         Content-Type: application/x-www-form-urlencoded\r\n\
Content-Length: 20\r\n\
Accept-Language: zh-cn\r\n\
Host: http://localhost:6000\r\n\r\n\
type=12345&name=aaaa\r\n\r\n");

解决方案 »

  1.   

    现在浏览器里边发送一个http post请求,然后用抓包工具wireshark抓包,将获取的http post请求字符串拷贝到代码里边就可以了
      

  2.   

    那如何在浏览器里发post请求呢?
      

  3.   

    你在csdn回帖就是一个http post 请求,在你的asp页面做一个啊
      

  4.   

    asp不是自己做的,不知道怎么做
      

  5.   

    看RFC啊
    www.w3.org/Protocols/rfc2616/rfc2616.html
      

  6.   

    正在看RFC2616呢,上面写得也不详细!
      

  7.   

    随便抓一个http post包不就了解了,对着rfc看
      

  8.   

    这样试试看
    sprintf(request, "POST /default.asp HTTP/1.1\r\n\
    Content-Length: 20\r\n\
    Host: localhost\r\n\r\n\
    type=12345&name=aaaa\r\n\r\n"
    );
      

  9.   

    sniffer抓包看一下就知道了。好似POST/GET格式是一样的,只不过是动词不一样而已。
      

  10.   

    请完整填写HTTP POST/GET的一些必要HTTP头,比如Referer.
      

  11.   

    对request的响应也是各个服务器不一样的,有些服务器需要额外的一些信息,这不是你简单post协议就能解决的。建议用一些高端对象来做,而不是自己手工发,在ASP种,可以试试Xmlhttp/HttpWebRequest
      

  12.   

    Content-Length:   20\r\n\ 
    Accept-Language:   zh-cn\r\n\ 
    Host:   http://localhost:6000\r\n\r\n\ 
    type=12345&name=aaaa\r\n\r\n ");想问一下Content-Length写的是20,后面实际给了多少?这上面至少是24,甚至是25,不是么?
      

  13.   

    几个必要字段格式。"POST %s HTTP/1.1\r\n" \
    "Accept: */*\r\n" \
    "Content-Type: application/x-www-form-urlencoded\r\n" \
    "User-Agent: Mozilla/4.0 (compatible; MPUD %s)\r\n" \
    "Host: %s\r\n" \"Cache-Control: no-cache\r\n" \
    "Connection: Keep-Alive\r\n" \
    "Content-Length: %d\r\n" \
    "\r\n"
    "%s"
    我登录VCKBASE抓的包如下,注意换行符。
    POST /SYS/login/login.aspx HTTP/1.1
    Host: www.vckbase.com
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: zh-cn,zh;q=0.5
    Accept-Encoding: gzip,deflate
    Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 300
    Connection: keep-alive
    Referer: http://www.vckbase.com/
    Cookie: ASPSESSIONIDCSAATTCD=DOMMILABJOPANJPNNAKAMCPK
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 79userid=aaaaaaa&password=01234567890&gclsid=501&imageField3.x=43&imageField3.y=11
      

  14.   

    1.利用libcurl写httpclient模块,支持http ftp等多种协议。
    2.winsock自己写,connect,send等。
    两中方案我都实现过,包括cookie管理
      

  15.   

    不用看文档,直接用ie操作一遍抓包,把抓到的包内容复制到你的request数组里,然后用socket发送就oksprintf(request,   "POST   http://localhost:6000/default.asp   HTTP/1.1\r\n\ 
    Accept:   */*\r\n\ 
                                        User-Agent:   MSIE   6.0\r\n\ 
                      Content-Type:   application/x-www-form-urlencoded\r\n\ 
    Content-Length:   20\r\n\ 
    Accept-Language:   zh-cn\r\n\ 
    Host:   http://localhost:6000\r\n\r\n\ 
    type=12345&name=aaaa\r\n\r\n ");通常这种用2个包发送,最后一句独立一个包,这也可以抓包看到
    这是最简单的做法
      

  16.   

    用Http Analyzer 抓下包,一看就知道了. 
      

  17.   

    顶LZ。。我在做代理 的时候遇到过类似的问题。。
    借宝地也问一下:
    在向163.com进行邮箱登陆的时候发出的请求为什么是 reg 而不是post。如下:
    "reg  http://localhost:6000/default.asp  HTTP/1.1\r\n\
    Accept:  */*\r\n\
                                        User-Agent:  MSIE  6.0\r\n\
                      Content-Type:  application/x-www-form-urlencoded\r\n\
    Content-Length:  20\r\n\
    Accept-Language:  zh-cn\r\n\
    Host:  http://www.163.com\r\n\r\n\
    type=12345&name=aaaa\r\n\r\n "初学,各位见笑了。。谢谢先。
      

  18.   

    http请求的构造是比较简单的
    第一行:请求方式 资源位置 HTTP/版本号
    每行表达一个意思
    空行(实际上有两对\r\n)就算请求头结束了GET方式就是上面的情况如果为POST,表明请求头后面还有附加数据,分两种情况:application/x-www-form-urlencoded 和 multipart/form-data
    前者如上面很多帖子所说,在请求头中告诉服务器附加数据的长度(Content-Length)就可以了,考虑到webserver会对名值对自动解析,填写要规范,不安全的字符要编码
    后者为大块多块数据(比如上传文件)的处理,需要进行边界(boundary)处理用 sniffer 抓包看看能更直观地了解楼上说163.com用的是reg方式,其实什么都无所谓,只要它的webwerver认识就行(它总是要看看你的帽子戴得对不对),以SOCKET的眼光来看,就是send,recv
      

  19.   

    POST /SYS/login/login.aspx HTTP/1.1
    Host: www.vckbase.com
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: zh-cn,zh;q=0.5
    Accept-Encoding: gzip,deflate
    Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 300
    Connection: keep-alive
    Referer: http://www.vckbase.com/
    Cookie: ASPSESSIONIDCSAATTCD=DOMMILABJOPANJPNNAKAMCPK
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 79userid=aaaaaaa&password=01234567890&gclsid=501&imageField3.x=43&imageField3.y=1124楼的这个就是例子。需要特别注意的是Content-Length后的数据长度值一定要正确,是获取的数据体部分的字节长度,具体就是 userid=aaaaaaa&password=01234567890&gclsid=501&imageField3.x=43&imageField3.y=11 这部分对应的数据。
      

  20.   

    除了研究的目的,有很多现成的http库,为什么不用??
      

  21.   

    参见本页:
    http://www.wantsoft.com/article/45.htm
      

  22.   

    楼主居然说 http://www.w3.org/standards/techs/http#w3c_all 不详细人家可是制订协议的人。
      

  23.   

    HTTP的发展是万维网协会(World Wide Web Consortium)和Internet工作小组(Internet Engineering Task Force)合作的结果,(他们)最终发布了一系列的RFC,其中最著名的就是RFC 2616。RFC 2616定义了HTTP协议的我们今天普遍使用的一个版本——HTTP 1.1。
      

  24.   

    http://download.csdn.net/source/2225088
    这个是我写的,使用VC++2008写的,绝对正确,现在项目一直在使用,你可以下下来参考一下。。
      

  25.   

    现在浏览器里边发送一个http   post请求,然后用抓包工具wireshark抓包,将获取的http   post请求字符串拷贝到代码里边就可以了
      

  26.   

    sprintf(Request,"POST /login.ashx  HTTP/1.1\n"
    "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8\n"
    "Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1,us-ascii\n"
    "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQPinyin 722; .NET CLR 2.0.50727)\n"
    "Host: xxx.xxx.cn\n"
    "Connection: Keep-Alive\n"
    "Content-Length:%d\n\n"
    "%s",strlen(Body)+1,Body);
      

  27.   

    "POST %s HTTP/1.1\r\n" \
    "Accept: */*\r\n" \
    "Content-Type: application/x-www-form-urlencoded\r\n" \
    "User-Agent: Mozilla/4.0 (compatible; MPUD %s)\r\n" \
    "Host: %s\r\n" \"Cache-Control: no-cache\r\n" \
    "Connection: Keep-Alive\r\n" \
    "Content-Length: %d\r\n" \
    "\r\n"
    "%s"
      

  28.   

    回复一个看看,是不是真的是POST方法
      

  29.   

    i don't know how to process '=' and ' '.