using System; |
using System.Collections.Generic; |
using System.IO; |
using System.Linq; |
using System.Net; |
using System.Text; |
using System.Web; |
public class HttpUtil |
{ |
/// <summary> |
/// 默认按keyValuePair的方式传输 http POST |
/// </summary> |
/// <param name="postUrl">URL</param> |
/// <param name="postDataStr">数据</param> |
/// <returns>返回内容</returns> |
public static ReturnInfo SendPost( string postUrl, string postDataStr, string contentType = "application/x-www-form-urlencoded" ) |
{ |
System.Net.ServicePointManager.DefaultConnectionLimit = 100; |
System.Net.ServicePointManager.Expect100Continue = false ; |
ReturnInfo _returnInfo = new ReturnInfo(); |
//用来存放cookie |
HttpWebRequest request = null ; |
Stream myRequestStream = null ; |
HttpWebResponse response = null ; |
Stream myResponseStream = null ; |
StreamReader myStreamReader = null ; |
try |
{ |
//转化 |
Console.WriteLine( "httppost:" + postDataStr); |
byte [] byteArray = Encoding.UTF8.GetBytes(postDataStr); |
//发送一个POST请求 |
request = (HttpWebRequest)WebRequest.Create( new Uri(postUrl)); |
request.Timeout = 30000; |
request.Method = "POST" ; |
if (contentType == null || contentType == "" ) |
{ |
contentType = "application/x-www-form-urlencoded" ; |
} |
request.ContentType = contentType; |
request.ContentLength = byteArray.Length; |
myRequestStream = request.GetRequestStream(); |
myRequestStream.Write(byteArray, 0, byteArray.Length); |
myRequestStream.Close(); |
//获取返回的内容 |
response = (HttpWebResponse)request.GetResponse(); |
myResponseStream = response.GetResponseStream(); |
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding( "utf-8" )); |
_returnInfo.Successful = true ; |
_returnInfo.ErrorMsg = "" ; |
_returnInfo.ReturnObj = myStreamReader.ReadToEnd(); |
} |
catch (Exception ex) |
{ |
_returnInfo.Successful = false ; |
_returnInfo.ErrorMsg = ex.Message; |
_returnInfo.ReturnObj = null ; |
} |
finally |
{ |
if (myStreamReader != null ) |
{ |
myStreamReader.Close(); |
} |
if (myResponseStream != null ) |
{ |
myResponseStream.Close(); |
} |
if (response != null ) |
{ |
response.Close(); |
} |
if (request != null ) |
{ |
request.Abort(); |
} |
} |
return _returnInfo; |
} |
/// <summary> |
/// 将数据组装成 key=UrlEncode(value)&key2=UrlEncode(value2),并httpPost |
/// </summary> |
/// <param name="postUrl">要发送的目标地址URI</param> |
/// <param name="postData">keyValuePair</param> |
/// <returns>传输结果</returns> |
public static ReturnInfo SendPost( string postUrl, Dictionary< string , string > postData) |
{ |
StringBuilder sb = new StringBuilder(); |
List< string > list = new List< string >(); |
foreach (var item in postData) |
{ |
if (item.Value == null ) |
{ |
continue ; |
} |
list.Add(item.Key + "=" + HttpUtility.UrlEncode(item.Value, System.Text.Encoding.UTF8)); |
} |
return SendPost(postUrl, string .Join( "&" , list), "" ); |
} |
} |
public class ReturnInfo |
{ |
public ReturnInfo() { } |
public ReturnInfo( bool successful, string errormsg, object returnobj = null ) |
{ |
this .Successful = successful; |
this .ErrorMsg = errormsg; |
this .ReturnObj = returnobj; |
} |
public bool Successful { get ; set ; } |
public string ErrorMsg { get ; set ; } |
public object ReturnObj { get ; set ; } |
} |
by: 发表于:2017-12-20 17:30:45 顶(0) | 踩(0) 回复
??
回复评论