我的HttpWebRequest POST请求有什么问题

时间:2013-03-25 01:42:16

标签: c# post httpwebrequest

POST个http包发送到网站时遇到问题,在屏幕截图中看起来像http://data2.floomby.com/files/previews/25_3_2013/KUi91GEdh02qmEM94LQtw.jpg。我想,有些东西在发送之前混合了我的帖子内容数据,但postdata字符串看起来很正常。 这是我的方法代码:

    public ResponseData AdvancedRequest(string method, string url, string postdata, string proxydata, string referer, int timeout)
    {
        var req = WebRequest.Create(url) as HttpWebRequest;
        req.Method = "GET";
        IWebProxy myProxy = new WebProxy(proxydata.Split(':')[0],  Convert.ToInt32(proxydata.Split(':')[1]));
        req.Proxy = null;
        //req.ServicePoint.ConnectionLimit = 1;
        req.ProtocolVersion = HttpVersion.Version10;
        req.KeepAlive = true;
        //req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0";
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0";
        req.AllowAutoRedirect = false;
        req.Timeout = timeout*1000;
        req.ReadWriteTimeout = timeout*1000;
        req.ContentType = "application/x-www-form-urlencoded";
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        req.Headers.Add("Accept-Language", "en-US,en;q=0.5");
        //req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        req.Headers.Add("Accept-Encoding", "gzip, deflate");
        //req.Headers.Add("cache-control", "max-age=0");
        req.CookieContainer = _cookies;
        //req.Headers.Add("Keep-Alive", "115");
        //req.Connection = "keepalive";
        if (!String.IsNullOrEmpty(referer))
        {
            req.Referer = referer;
        }

        req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        if (method == "POST")
        {
            req.Method = "POST";
            req.ServicePoint.Expect100Continue = false;
            System.Text.Encoding enc = System.Text.Encoding.UTF8;
            var bytedata = enc.GetBytes(postdata);
            req.ContentLength = bytedata.Length;
            //Stream postStream = req.GetRequestStream();
            try
            {
                using (var postStream = req.GetRequestStream())
                {
                    postStream.Write(bytedata, 0, bytedata.Length);
                    postStream.Close();
                }
            }
            catch (WebException except)
            {

            }

        }

        HttpWebResponse resp = (HttpWebResponse)req.GetResponse() as HttpWebResponse;
        this._cookies.Add(resp.Cookies);
        //resp.Close();
        ResponseData rdata = new ResponseData(resp);
        resp.Close();
        return rdata;
        //StreamReader strdr = new StreamReader(resp.GetResponseStream());
        //string response = strdr.ReadToEnd();
        //strdr.Close();

        //return response;
    }

这是正确的帖子数据:

  

会话%5Busername_or_email%5D = LoginHere&安培;会话%5Bpassword%5D = PasswordHere&安培; return_to_ssl =真安培; scribe_log =安培; redirect_after_login =%2F&安培; authenticity_token = auth_token_here

以下是错误的帖子数据:

  

会话%5Busername_or_email%5D =的AsyncCallback&安培;会话%5Bpassword%5D = De2zmcc6093&安培; return_to_ssl =真安培; scribe_log =安培; redirect_after_login =%2F&安培; authenticity_token = 0e67f8df715c79f81a9b2be203848ad7f805b21csession%5Busername_or_email%5D =的AsyncCallback&安培;会话%5Bpassword%5D = De2zmcc6093&安培; return_to_ssl =真安培; scribe_log =安培; redirect_after_login =%2F&安培; authenticity_token = 0e67f8df715c79f81a9b2be203848ad7f805b21c

postdata的值为:

  

会话%5Busername_or_email%5D = LOGIN &安培;会话%5Bpassword%5D = PASSWORD &安培; return_to_ssl =真安培; scribe_log =安培; redirect_after_login =%2F&安培; authenticity_token = 0e67f8df715c79f81a9b2be203848ad7f805b21c

0 个答案:

没有答案