直接上代碼web
後臺代碼json
//接口地址
string url = "http://spherefg.topsmoon.com:6666/restapi/Comment/SubmitCommentFromPC?commentPeople=1067&comment=%E4%B8%AD%E8%AF%84&content=%E6%B5%8B%E8%AF%95&taskId=1160&userId=1"; //Get請求 HttpWebResponse w = HttpHelper.CreateGetHttpResponse(url, 30000, null, null); StreamReader sr = new StreamReader(w.GetResponseStream()); string/*這是Json字符串*/ jsonstr = sr.ReadToEnd();
工具類:api
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; public class HttpHelper { /// <summary> /// 建立GET方式的HTTP請求 /// </summary> /// public static HttpWebResponse CreateGetHttpResponse(string url, int timeout, string userAgent, CookieCollection cookies) { HttpWebRequest request = null; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { //對服務端證書進行有效性校驗(非第三方權威機構頒發的證書,如本身生成的,不進行驗證,這裏返回true) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; //http版本,默認是1.1,這裏設置爲1.0 } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "GET"; //設置代理UserAgent和超時 //request.UserAgent = userAgent; //request.Timeout = timeout; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } return request.GetResponse() as HttpWebResponse; } /// <summary> /// 建立POST方式的HTTP請求 /// </summary> public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int timeout, string userAgent, CookieCollection cookies) { HttpWebRequest request = null; //若是是發送HTTPS請求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; //request.ProtocolVersion = HttpVersion.Version10; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; if (parameters == null) { request.ContentLength = 0; } //設置代理UserAgent和超時 //request.UserAgent = userAgent; //request.Timeout = timeout; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } //發送POST數據 if (!(parameters == null || parameters.Count == 0)) { StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, parameters[key]); } else { buffer.AppendFormat("{0}={1}", key, parameters[key]); i++; } } byte[] data = Encoding.ASCII.GetBytes(buffer.ToString()); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } string[] values = request.Headers.GetValues("Content-Type"); return request.GetResponse() as HttpWebResponse; } /// <summary> /// 獲取請求的數據 /// </summary> public static string GetResponseString(HttpWebResponse webresponse) { using (Stream s = webresponse.GetResponseStream()) { StreamReader reader = new StreamReader(s, Encoding.UTF8); return reader.ReadToEnd(); } } /// <summary> /// 驗證證書 /// </summary> private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { if (errors == SslPolicyErrors.None) return true; return false; } }