鏈接微服務

鏈接微服務,json傳輸html

public static string HttpPost(string posturl, string postData, string method = "POST", string contentType = "application/json")
{
Encoding encoding = Encoding.GetEncoding("utf-8");
byte[] data = encoding.GetBytes(postData);
try
{
// 設置參數
HttpWebRequest request = WebRequest.Create(posturl) as HttpWebRequest;
//增長下面兩個屬性便可
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = method;
request.ServicePoint.Expect100Continue = false;
request.ContentType = contentType;
request.ContentLength = data.Length;
byte[] buffer = encoding.GetBytes(postData); // 數據編碼UTF-8格式
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
return reader.ReadToEnd();
}
}
catch (Exception ex)
{
return string.Empty;
}
}json

傳輸string瀏覽器

public static string Http(string Url, string Data, string Method)
{
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = Url,//URL 必需項
Method = Method,//URL 可選項 默認爲Get
IsToLower = false,//獲得的HTML代碼是否轉成小寫 可選項默認轉小寫
Cookie = "",//字符串Cookie 可選項
Referer = "",//來源URL 可選項
Postdata = Data,//Post數據 可選項GET時不須要寫
Timeout = 100000,//鏈接超時時間 可選項默認爲100000
ReadWriteTimeout = 30000,//寫入Post數據超時時間 可選項默認爲30000
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用戶的瀏覽器類型,版本,操做系統 可選項有默認值
ContentType = "application/x-www-form-urlencoded",//返回類型 可選項有默認值
Allowautoredirect = false,//是否根據301跳轉 可選項
//CerPath = "d:\123.cer",//證書絕對路徑 可選項不須要證書時能夠不寫這個參數
//Connectionlimit = 1024,//最大鏈接數 可選項 默認爲1024
ProxyIp = "",//代理服務器ID 可選項 不須要代理 時能夠不設置這三個參數
//ProxyPwd = "123456",//代理服務器密碼 可選項
//ProxyUserName = "administrator",//代理服務器帳戶名 可選項
ResultType = ResultType.String
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
string cookie = result.Cookie;
return html;
}服務器

相關文章
相關標籤/搜索