. Net integration of Json to implement the REST service client method details, jsonrest
Preface
This article mainly introduces you to the REST service client using. Net to integrate Json, and shares the content for your reference. I will not talk about it below. Let's take a look at the detailed introduction.
I. Preparations
1. Click the official website or download the. Net4.0 Json plug-in Newtonsoft. Json.
2. Find % compressed package % \ Bin \ net40 \ Newtonsoft. Json. dll and reference it in the project.
Ii. Related code Introduction
1. HttpClientUtil. cs encapsulate the REST Method
Using Newtonsoft. json; using System. collections. generic; using System. IO; using System. linq; using System. net; using System. text; namespace psi. common {public class HttpClientUtil {// REST @ GET method, which is automatically converted to an object based on the generic type. List <T> public static T doGetMethodToObj <T> (string url) is supported) {HttpWebRequest request = (HttpWebRequest) WebRequest. create (url); request. method = "get"; request. contentType = "application/json; charset = UTF-8"; HttpWebResponse response = null; try {response = (HttpWebResponse) request. getResponse ();} catch (WebException e) {response = (HttpWebResponse) e. response; return default (T);} string json = getResponseString (response); return JsonConvert. deserializeObject <T> (json);} // convert the returned result of HttpWebResponse to string private static string getResponseString (HttpWebResponse response) {string json = null; using (StreamReader reader = new StreamReader (response. getResponseStream (), System. text. encoding. getEncoding ("UTF-8") {json = reader. readToEnd () ;}return json;} // obtain the exception information private static string getRestErrorMessage (HttpWebResponse errorResponse) {string errorhtml = getResponseString (errorResponse); string errorkey = "UnhandledException :"; errorhtml = errorhtml. substring (errorhtml. indexOf (errorkey) + errorkey. length); errorhtml = errorhtml. substring (0, errorhtml. indexOf ("\ n"); return errorhtml;} // REST @ POST method public static T doPostMethodToObj <T> (string url, string jsonBody) {HttpWebRequest request = (HttpWebRequest) webRequest. create (url); request. method = "post"; request. contentType = "application/json; charsets = UTF-8"; var stream = request. getRequestStream (); using (var writer = new StreamWriter (stream) {writer. write (jsonBody); writer. flush ();} HttpWebResponse response = (HttpWebResponse) request. getResponse (); string json = getResponseString (response); return JsonConvert. deserializeObject <T> (json);} // REST @ PUT method public static string doPutMethod (string url) {HttpWebRequest request = (HttpWebRequest) WebRequest. create (url); request. method = "put"; request. contentType = "application/json; charset = UTF-8"; HttpWebResponse response = (HttpWebResponse) request. getResponse (); using (StreamReader reader = new StreamReader (response. getResponseStream (), System. text. encoding. getEncoding ("UTF-8") {return reader. readToEnd () ;}}// REST @ PUT method, with the public static T doPutMethodToObj (string url, string jsonBody) {HttpWebRequest request = (HttpWebRequest) WebRequest. create (url); request. method = "put"; request. contentType = "application/json; charset = UTF-8"; request. timeout = 30*1000; var stream = request. getRequestStream (); using (var writer = new StreamWriter (stream) {writer. write (jsonBody); writer. flush ();} HttpWebResponse response = (HttpWebResponse) request. getResponse (); string json = getResponseString (response); return JsonConvert. deserializeObject <T> (json);} // REST @ DELETE method public static bool doDeleteMethod (string url) {HttpWebRequest request = (HttpWebRequest) WebRequest. create (url); request. method = "delete"; request. contentType = "application/json; charset = UTF-8"; HttpWebResponse response = (HttpWebResponse) request. getResponse (); using (StreamReader reader = new StreamReader (response. getResponseStream (), System. text. encoding. getEncoding ("UTF-8") {string responseString = reader. readToEnd (); if (responseString. equals ("1") {return true;} return false ;}}}}
2. Call the REST server method in Json format.
/// <Summary> /// obtain the url of the upgraded server. /// </summary> /// <returns> </returns> private String getServerUrl () {String result = ""; UpgraderClient upgraderClient = getUpgraderClient (); if (upgraderClient! = Null) {result + = "http: //" + upgraderClient. serverIP + ":" + upgraderClient. serverPort + "/upgraderServer/service/upgrade. do ";} return result ;} /// <summary> /// test and upgrade the server connection // </summary> /// <returns> </returns> public bool testConnect () {FileRequest fileReq = new FileRequest (); fileReq. type = (int) RequestType. TEST_CONNECT; string jsonData = JsonConvert. serializeObject (fileReq); FileResponse rep = Null; try {rep = HttpClientUtil. doPostMethodToObj <FileResponse> (getServerUrl (), jsonData);} catch {throw new Exception ("failed to connect to the remote server! ");} Return rep. status = 200 ;}
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.