Newtonsoft.Json.dll
1 object o = WebServiceHelper.doPostMethodToObj<object>("http://……………………", "{"username":"" + this.tbx_account.Text.Trim() + "","password":"" + this.tbx_pwd.Password.Trim() + ""}"); 2 JObject jo = o as JObject;
1 using System; 2 using System.Web.Services.Description; 3 using System.IO; 4 using System.Net; 5 using System.Text; 6 using System.CodeDom; 7 using System.CodeDom.Compiler; 8 using Microsoft.CSharp; 9 using System.Windows.Forms; 10 using Newtonsoft.Json; 11 12 namespace ConsoleApplication1 13 { 14 public class WebServiceHelper 15 { 16 17 18 // REST @GET 方法,根据泛型自动转换成实体,支持List<T> 19 public static T doGetMethodToObj<T>(string metodUrl) 20 { 21 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(metodUrl); 22 request.Method = "get"; 23 request.ContentType = "application/json;charset=UTF-8"; 24 HttpWebResponse response = null; 25 try 26 { 27 response = (HttpWebResponse)request.GetResponse(); 28 } 29 catch (WebException e) 30 { 31 response = (HttpWebResponse)e.Response; 32 MessageBox.Show(e.Message + " - " + getRestErrorMessage(response)); 33 return default(T); 34 } 35 string json = getResponseString(response); 36 return JsonConvert.DeserializeObject<T>(json); 37 } 38 39 // 将 HttpWebResponse 返回结果转换成 string 40 private static string getResponseString(HttpWebResponse response) 41 { 42 string json = null; 43 using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"))) 44 { 45 json = reader.ReadToEnd(); 46 } 47 return json; 48 } 49 50 // 获取异常信息 51 private static string getRestErrorMessage(HttpWebResponse errorResponse) 52 { 53 string errorhtml = getResponseString(errorResponse); 54 string errorkey = "spi.UnhandledException:"; 55 errorhtml = errorhtml.Substring(errorhtml.IndexOf(errorkey) + errorkey.Length); 56 errorhtml = errorhtml.Substring(0, errorhtml.IndexOf(" ")); 57 return errorhtml; 58 } 59 60 // REST @POST 方法 61 public static T doPostMethodToObj<T>(string metodUrl, string jsonBody) 62 { 63 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(metodUrl); 64 request.Method = "post"; 65 request.ContentType = "application/json;charset=UTF-8"; 66 var stream = request.GetRequestStream(); 67 using (var writer = new StreamWriter(stream)) 68 { 69 writer.Write(jsonBody); 70 writer.Flush(); 71 } 72 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 73 string json = getResponseString(response); 74 return JsonConvert.DeserializeObject<T>(json); 75 } 76 77 // REST @PUT 方法 78 public static string doPutMethod(string metodUrl) 79 { 80 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(metodUrl); 81 request.Method = "put"; 82 request.ContentType = "application/json;charset=UTF-8"; 83 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 84 using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"))) 85 { 86 return reader.ReadToEnd(); 87 } 88 } 89 90 // REST @PUT 方法,带发送内容主体 91 public static T doPutMethodToObj<T>(string metodUrl, string jsonBody) 92 { 93 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(metodUrl); 94 request.Method = "put"; 95 request.ContentType = "application/json;charset=UTF-8"; 96 var stream = request.GetRequestStream(); 97 using (var writer = new StreamWriter(stream)) 98 { 99 writer.Write(jsonBody); 100 writer.Flush(); 101 } 102 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 103 string json = getResponseString(response); 104 return JsonConvert.DeserializeObject<T>(json); 105 } 106 107 // REST @DELETE 方法 108 public static bool doDeleteMethod(string metodUrl) 109 { 110 HttpWebRequest request = (HttpWebRequest)WebRequest.Create( metodUrl); 111 request.Method = "delete"; 112 request.ContentType = "application/json;charset=UTF-8"; 113 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 114 using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"))) 115 { 116 string responseString = reader.ReadToEnd(); 117 if (responseString.ToUpper().ToString()=="TRUE") 118 { 119 return true; 120 } 121 return false; 122 } 123 } 124 } 125 126 }