zoukankan      html  css  js  c++  java
  • HttpWebRequest POST 数据时请求头多了一行Expect: 100continue,少了数据行

    用HttpWebRequest   POST数据时POST数据被截掉了。使用HTTPLook跟踪出来的HTTP头信息  
      POST   /sp/SPLogin   HTTP/1.1  
      Accept:   image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,   application/msword,   application/x-shockwave-flash,   */*  
      User-Agent:   Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.2;   SV1;   .NET   CLR   1.1.4322  
      Accept-Language:   zh-cn  
      UA-CPU:   x86  
      Accept-Encoding:   gzip,   deflate  
      Content-Type:   application/x-www-form-urlencoded  
      Content-Length:   85  
      Expect:   100-continue  
      Connection:   Keep-Alive  
      Host:   test.com  
      Cookie:   JSESSIONID=Ghdht2vhW2xTPcFRYG1ch1Xt1Kp81rRGvR11nlQNKW6l4MTZ1MHk!-1870340995  
       
      正常情况应为:  
      POST   /sp/SPLogin   HTTP/1.1  
      Accept:   image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,   application/msword,   application/x-shockwave-flash,   */*  
      User-Agent:   Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.2;   SV1;   .NET   CLR   1.1.4322  
      Accept-Language:   zh-cn  
      UA-CPU:   x86  
      Accept-Encoding:   gzip,   deflate  
      Content-Type:   application/x-www-form-urlencoded  
      Content-Length:   85  
      Connection:   Keep-Alive  
      Host:   admin.sh.monternet.com  
      Cookie:   JSESSIONID=Ghdht2vhW2xTPcFRYG1ch1Xt1Kp81rRGvR11nlQNKW6l4MTZ1MHk!-1870340995  
       
      selectAccount=SPPREREG&USER=907000&PASSWORD=111111&addCode=9146&Submit22=%B5%C7%C2%BC  
       
       
      以下是程序代码:  
      public   static   void   GetWebContent(string   url,CookieContainer   cookies,string   filename)  
      {  
            HttpWebRequest   request   =   null;  
            HttpWebResponse   response   =   null;  
            CookieCollection   cookieResponse   =   new   CookieCollection();  
            CookieContainer   cookieRequest   =   new   CookieContainer();  
            Uri   requestUri   =   new   Uri(url);  
            try  
            {  
                  Cookie   cookie   =   new   Cookie();  
                  cookie.Name   =   "JSESSIONID";  
                  cookie.Value   =   "Ghdht2vhW2xTPcFRYG1ch1Xt1Kp81rRGvR11nlQNKW6l4MTZ1MHk!-1870340995";  
                  cookie.Path   =   "/";  
                  cookie.Domain   =requestUri.Host;  
                  cookieRequest.Add(cookie);  
       
                  //   处理请求  
                  request   =   (HttpWebRequest)   WebRequest.Create(new   Uri(url));  
                  request.Accept   ="image/gif,   image/x-xbitmap,   image/jpeg,   image/pjpeg,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,   application/msword,   application/x-shockwave-flash,   */*";  
                  request.UserAgent   =   "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.2;   SV1;   .NET   CLR   1.1.4322";  
                  request.Headers.Add("Accept-Language:   zh-cn");  
                  request.Headers.Add("UA-CPU:   x86");  
                  request.Headers.Add("Accept-Encoding:   gzip,   deflate");  
                  request.Timeout   =   20000;  
                  request.CookieContainer   =   cookieRequest;  
       
                  string   data   =   "selectAccount=SPPREREG&USER=907000&PASSWORD=111111&addCode=9146&Submit22=%B5%C7%C2%BC";  
                  byte[]   byteData   =   Encoding.Default.GetBytes(data);  
                  request.Method   ="POST";  
                  request.ContentType   =   "application/x-www-form-urlencoded";  
                  request.ContentLength   =   byteData.Length;  
                  using   (Stream   requestStream   =   request.GetRequestStream())  
                  {  
      requestStream.Write(byteData,0,byteData.Length);  
                  }  
       
                  //   处理响应  
                  response   =   (HttpWebResponse)request.GetResponse();  
                  cookieResponse   =   response.Cookies   ;  
                  byte[]   buffer   =     ResponseAsBytes(response);  
                  response.Close();  
       
                  FileStream   file   =   new   FileStream(filename,   FileMode.Create);  
                  file.Write(buffer,0,buffer.Length);  
       
             
            }catch(Exception   ex)  
            {  
                  Log.Error("GetWeb:Url="+url   +   ex.Message,ex);  
            }  
      }  
      那位知道原因吗??  



    自己已经搞定,设置一个属性  
      request.ServicePoint.Expect100Continue   =   false;   
     
  • 相关阅读:
    java常见异常归纳
    内存泄露和内存溢出
    2015年工作中遇到的问题91-100
    WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path
    WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path
    Mongodb总结6-数据库启动、停止、备份等命令
    Mongodb总结5-通过装饰模式,用Mongodb解决Hbase的不稳定问题
    Mongodb总结5-通过装饰模式,用Mongodb解决Hbase的不稳定问题
    设置Maven默认的JDK为1.7,解决Update Maven Project默认为1.5和Maven打包报错2个问题
    设置Maven默认的JDK为1.7,解决Update Maven Project默认为1.5和Maven打包报错2个问题
  • 原文地址:https://www.cnblogs.com/goody9807/p/1190370.html
Copyright © 2011-2022 走看看