=============================================================================================================== =======================///<summary>///Date: 2016-2-4///Note: The bug has been modified to use///</summary> public static void Met Hod1 () { try { String domain = "http://192.168.1.6:8098/";
String url = domain + "/signin/loginapi";
HttpWebRequest request = (HttpWebRequest) webrequest.create (URL); request.
method = "POST"; request.
ContentType = "application/x-www-form-urlencoded"; Request.
ContentType = "Application/json"; request.
Readwritetimeout = 30 * 1000; ///add parameter dictionary<string, string> diclist = new dictionary& Lt
String, string> ();
Diclist.add ("UserName", "test@qq.com");  
Diclist.add ("Password", "000000");
String poststr = Buildquerystr (diclist);
byte[] data = Encoding.UTF8.GetBytes (POSTSTR); request. ContentLength = data.
Length; Stream myrequeststream = Request.
GetRequestStream (); myrequeststream.write (data, 0, data.
Length);
myrequeststream.close (); HttpWebResponse response = (HttpWebResponse) request.
GetResponse (); StreamReader mystreamreader = new StreamReader (response.
GetResponseStream (), Encoding.UTF8);
var retstring = Mystreamreader.readtoend ();
mystreamreader.close (); } catch (Exception ex) { log.
Info ("entered Itemhierarchycontroller-initialize"); log. Error (ex.
message); }} ================================================================================================
======================================
upgrade version, extract to help class, encapsulate object
Using System;
Using System.Collections.Generic;
Using System.Configuration;
Using System.IO;
Using System.Net;
Using System.Text;
Using System.Web; Namespace Cms.common {public class Myhttpclient {public string methodurl = string.
Empty;
public string poststr = null;
Public myhttpclient (String methodurl) {this.methodurl = Methodurl; Public Myhttpclient (String methodurl, String poststr) {///this.methodurl = Configurationma Nager.
appsettings["Apifrontend"];///http://192.168.1.6:8098/signin/loginapi///this.poststr = postStr;
This.methodurl = Methodurl;
This.poststr = Poststr; }///<summary>///get method///</summary>///<returns></returns > Public String executeget () {HttpWebRequest myrequest = (HttpWebRequest) webrequest.create
(This.methodurl); MyreqUest.
method = ' Get ';
HttpWebResponse myresponse = null;
try {myresponse = (HttpWebResponse) myrequest.getresponse ();
StreamReader reader = new StreamReader (Myresponse.getresponsestream (), Encoding.UTF8); String content = Reader.
ReadToEnd ();
return content; }//Exception request catch (WebException e) {myresponse = (HttpWebResponse) e.resp
Onse; using (Stream Errdata = Myresponse.getresponsestream ()) {using (StreamReader reader = New StreamReader (Errdata)) {string text = reader.
ReadToEnd ();
return text; }}}///<summary>///POST method///</summary&
Gt <returns></returns> public String ExecutEpost () {string content = string.
Empty;
Random rd = new Random (); int rd_i = Rd.
Next ();
String nonce = convert.tostring (rd_i);
String timestamp = convert.tostring (Convertdatetimeint (DateTime.Now));
String signature = Gethash (This.appsecret + nonce + timestamp);
try {HttpWebRequest request = (HttpWebRequest) webrequest.create (This.methodurl); Request.
method = "POST"; Request.
ContentType = "application/x-www-form-urlencoded"; Request.
ContentType = "Application/json"; Request.
Headers.add ("Nonce", Nonce); Request.
Headers.add ("Timestamp", Convert.ToString (Stringproc.convertdatetimeint (DateTime.Now)); Request.
Headers.add ("Signature", Signature); Request.
Readwritetimeout = 30 * 1000;
byte[] data = Encoding.UTF8.GetBytes (POSTSTR); Request. ContentLength = data.
Length; Stream Myrequeststream = Request.
GetRequestStream (); Myrequeststream.write (data, 0, data.)
Length);
Myrequeststream.close (); HttpWebResponse response = (HttpWebResponse) request.
GetResponse (); StreamReader Mystreamreader = new StreamReader (response.
GetResponseStream (), Encoding.UTF8);
Content = Mystreamreader.readtoend ();
Mystreamreader.close ();
The catch (Exception ex) {} return content; } public class Stringproc {public static String buildquerystr (dictionary<string, string> dic
List) {String poststr = ""; foreach (var item in diclist) {Poststr = + item. Key + "=" + Httputility.urlencode (item.
Value, Encoding.UTF8) + "&"; } poststr = poststr.substring (0, Poststr.lastindexof (' & '));
return poststr; public static int Convertdatetimeint (System.DateTime time) {System.DateTime starttime = Ti
MeZone.CurrentTimeZone.ToLocalTime (New System.DateTime (1970, 1, 1)); return (int) (time-starttime).
TotalSeconds; }
}
}
Front-End Call
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using Cms.common;
Using Newtonsoft.json; Namespace Medicine.Web.Controllers {public class Defaultcontroller:controller {public ActionResult Ind Ex () {#region Doget string Getresultjson = this.
Doget (URL); Httpclientresult Customerresult = (httpclientresult) jsonconvert.deserializeobject (GetResultJson, typeof (
Httpclientresult));
#endregion #region DoPost String name = request.form["UserName"];
string password = request.form["password"];
dictionary<string, string> diclist = new dictionary<string, string> ();
Diclist.add ("UserName", name);
Diclist.add ("Password", Password);
String poststr = Stringproc.buildquerystr (diclist); String Postresultjson = this.
DoPost (URL, poststr); HttpclientreSult Userresult = (httpclientresult) jsonconvert.deserializeobject (Postresultjson, typeof (Httpclientresult));
#endregion return View (); }///<summary>///get method///</summary>///<param name= "Portraituri
">url address </param>///<returns></returns> private String Doget (String Portraituri)
{myhttpclient client = new Myhttpclient (Portraituri); Return client.
Executeget (); }///<summary>///POST method///</summary>///<param name= "Portraitur I ">url address </param>///<param name=" poststr "> Request parameters </param>///<returns></retur Ns> private String DoPost (String Portraituri, String poststr) {myhttpclient client = new M
Yhttpclient (Portraituri, POSTSTR); Return client.
Executepost (); } publicClass Httpclientresult {public string UserName {get; set;}
public bool Success {get; set;} }
}
}