C#Post提交

來源:互聯網
上載者:User

標籤:json格式   style   div   抓包   out   length   string   for   tor   

1、提交二進位流資料(json格式):

          var dic = new SortedDictionary<string, string>            {                {"name", "test"},                {"pass", "123"},            };                        //序列化參數            var jsonParam = JsonConvert.SerializeObject(dic);            //發送請求            var request = (HttpWebRequest)WebRequest.Create(Url);            request.Method = "POST";            request.ContentType = "application/json;charset=UTF-8";            var byteData = Encoding.UTF8.GetBytes(jsonParam);            var length = byteData.Length;            request.ContentLength = length;            var writer = request.GetRequestStream();            writer.Write(byteData, 0, length);            writer.Close();            //接收資料            var response = (HttpWebResponse)request.GetResponse();            var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();

 

2、提交表單方法一:

            var url = "xxxxxxxxxxxxxxxxxxxxxx";            var webClientObj = new WebClient();            var postVars = new NameValueCollection {                {"name", "test"},                {"pass", "123"}            };            byte[] byRemoteInfo = webClientObj.UploadValues(url, "POST", postVars);            string json = Encoding.UTF8.GetString(byRemoteInfo);

3、提交表單方法二:

var url = "xxxxxxxxxxxxxxxxxxxxxxx";            //通過抓包工具,可以看到是這個格式            var postData = "username=test&password=123";            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);            req.Method = "POST";            req.Timeout = 30000;            req.AllowAutoRedirect = false;            req.ContentType = "application/x-www-form-urlencoded";            req.KeepAlive = true;            byte[] postBytes = Encoding.UTF8.GetBytes(postData);            req.ContentLength = postBytes.Length;            Stream postDataStream = req.GetRequestStream();            postDataStream.Write(postBytes, 0, postBytes.Length);            postDataStream.Close();            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

 

C#Post提交

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.