1233333333333333333333333

C# http请求

 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 }
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 XXX;
 2 using Newtonsoft.Json.Linq;
 3 
 4 namespace XXXX
 5 {
 6     public partial class HandlerRequest : System.Web.UI.Page
 7     {
 8         protected void Page_Load(object sender, EventArgs e)
 9         {
10             JObject pobj = JObject.Parse(Request.Form["paramaters"]);
11             var Status = pobj["Status "];
12             var UserName = apply["UserName "];
13             //dosomething
14             Response.StatusDescription = "123";
15             Response.Write("123456");
16             Response.End();
17         }
18     }
19 }                
View Code

 

相关资料:http://www.cnblogs.com/kuangwu/p/3275584.html

posted @ 2015-06-14 13:28  hghrpg  阅读(138)  评论(0编辑  收藏  举报
1233333333333333333333333