C# http請求

來源:互聯網
上載者:User

標籤:

 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Net; 6 using System.Text; 7 using System.Threading.Tasks; 8  9 namespace Common10 {11     public class HttpHelper12     {13         public static string Post(string url, string jsonParams, string jsonParamsName) 14         {15             System.Net.WebRequest request = System.Net.WebRequest.Create(url);16             request.Method = "POST";17             request.ContentType = "application/x-www-form-urlencoded";18             string paraUrlCoded = System.Web.HttpUtility.UrlEncode(jsonParamsName);19             paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParams);20             byte[] byteArray = Encoding.UTF8.GetBytes(paraUrlCoded);21             request.ContentLength = byteArray.Length;22             Stream requestStream;23             try24             {25                 requestStream = request.GetRequestStream();26             }27             catch (Exception)28             {29                 requestStream = null;30                 Console.Write("串連伺服器失敗!");31             }32             requestStream.Write(byteArray, 0, byteArray.Length);33             requestStream.Close();34 35             System.Net.WebResponse response;36             try37             {38                 response = request.GetResponse();39             }40             catch (Exception ex)41             {42                 return ex.Message;43             }44             //Console.WriteLine(((System.Net.HttpWebResponse)response).StatusDescription);45             Stream responseStream = response.GetResponseStream();46             System.IO.StreamReader reader = new System.IO.StreamReader(responseStream);47             string responseFromServer = reader.ReadToEnd();48             reader.Close();49             requestStream.Close();50             response.Close();51             return responseFromServer;52         }53     }54 }
View Code

 

1 string testUrl = "http://XXXXXX/HandlerRequest.aspx";2             var temp = new { Status = "Pass", UserName= "test" };3             string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(temp);4             Common.HttpHelper.Post(testUrl, jsonString, "paramaters");
View Code

 

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using XXX; 8 using Newtonsoft.Json.Linq; 9 10 namespace XXXX11 {12     public partial class HandlerRequest : System.Web.UI.Page13     {14         protected void Page_Load(object sender, EventArgs e)15         {16             JObject pobj = JObject.Parse(Request.Form["paramaters"]);17             var apply = pobj["apply"];18             var wfType = apply["WorkFlow_Type"];19             var formID = apply["FormID"];20             //dosomething;21             Response.StatusDescription = "123";22             Response.Write("123456");23             Response.End();24         }25     }26 }
View Code

 

相關資料:http://www.cnblogs.com/kuangwu/p/3275584.html

C# http請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.