通过使用HttpWebRequest丢失帖子数据 [英] missing post data by using HttpWebRequest

查看:57
本文介绍了通过使用HttpWebRequest丢失帖子数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 HttpWebRequest 时发布数据时遇到问题a>.

I got a problem on posting data by using HttpWebRequest.

有一个字符串(即key1=value1&key2=value2&key3=value3),我已将其发布到站点(即www.*.com/edit),但是,我不知道为什么有时没什么错,但有时,第一个key=value1将丢失,只有key2=value&key3=value3可以在HttpAnalyzer中找到.

There is a string(ie. key1=value1&key2=value2&key3=value3) and I have post it to a site (ie. www.*.com/edit), but ,I don't know why that sometimes it's nothing wrong , but sometimes ,the first key=value1 will be missing, only key2=value&key3=value3 that can find in HttpAnalyzer.

public static string SubmitData(string Url, string FormData, CookieContainer _Cc, string ContentType)
        {
            Stream RequestStream = null, ResponseStream = null; StreamReader Sr = null;
            HttpWebRequest HRequest = (HttpWebRequest)WebRequest.Create(Url);
            try
            {

                HRequest.CookieContainer = _Cc;
                HRequest.Method = "POST";
                HRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)";
                HRequest.ContentType = ContentType;
                HRequest.ContentLength = FormData.Length;
                //byte[] BFromData = new ASCIIEncoding().GetBytes(FormData);
                byte[] BFromData = Encoding.ASCII.GetBytes(FormData);
                BFromData = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, BFromData);//ascii → utf8
                RequestStream = HRequest.GetRequestStream();
                RequestStream.Write(BFromData, 0, BFromData.Length);
                //RequestStream.Write(utf8Bytes,0,utf8Bytes.Length );
                HttpWebResponse HResponse = (HttpWebResponse)HRequest.GetResponse();
                ResponseStream = HResponse.GetResponseStream();
                Sr = new StreamReader(ResponseStream, Encoding.UTF8);
                return Sr.ReadToEnd();
            }
            catch
            {
                return "";
            }
            finally
            {
                if (null != RequestStream) RequestStream.Close();
                if (null != ResponseStream) ResponseStream.Close();
                if (null != Sr) Sr.Close();
            }
        }

推荐答案

使用提琴手来了解如何当您单击该表单,然后尝试使用此方法并修改您的请求所需的内容时,该请求看起来就像.

Use Fiddler to see how the request looks like when you click on the form then try using this approach and modify what you need for your request.

public static void PostDataAndDoSomething()
        {            
            string URI = "http://www.something.com";
            //make your request payload
            string requestBody = String.Format("{{'param1': {0}, 'param2': {1}}}",value1, value2); //json format

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI);  //make request         

            // set request headers as you need
            request.ContentType = "application/json; charset=UTF-8"; 
            request.Accept = "application/json, text/javascript;
            request.Method = "POST";            
            request.UserAgent = "";
            request.Headers.Add("X-Requested-With", "XMLHttpRequest");
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(requestBody); //write your request payload
            }

            WebResponse response = request.GetResponse();          
            string jsonData = String.Empty;

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                jsonData = reader.ReadToEnd();
            }           
            response.Close();

            //do something with your data, deserialize, Regex etc....
        }

这篇关于通过使用HttpWebRequest丢失帖子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆