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 Common 10 { 11 public class HttpHelper 12 { 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 try 24 { 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 try 37 { 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 }
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");
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 XXXX 11 { 12 public partial class HandlerRequest : System.Web.UI.Page 13 { 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 }
相关资料:http://www.cnblogs.com/kuangwu/p/3275584.html
时间: 2024-10-16 19:38:32