Howtogethttpresponse.-创新互联
成都创新互联公司服务项目包括高明网站建设、高明网站制作、高明网页制作以及高明网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,高明网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到高明省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!>public class HttpWebResponseUtility
{
public static string CreateGetHttpResponse(string url)
{
var response = CreateGetHttpResponse(url, null);
Stream dataStream= response.GetResponseStream();
StreamReader reader= new StreamReader(dataStream);
string responseString = reader.ReadToEnd();
return responseString;
}
public static string CreatePostHttpResponse(string url, IDictionary parameters)
{
var response = CreatePostHttpResponse(url, parameters,null,System.Text.Encoding.UTF8);
Stream dataStream= response.GetResponseStream();
StreamReader reader= new StreamReader(dataStream);
string responseString = reader.ReadToEnd();
return responseString;
}
public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
var request = WebRequest.Create(url) as HttpWebRequest;
request.Method= "GET";
request.Proxy= null;
request.Credentials= CredentialCache.DefaultCredentials;
//allows for validation of SSL certificates ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
if (timeout.HasValue)
{
request.Timeout= timeout.Value;
}
return request.GetResponse() as HttpWebResponse;
}
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary parameters, int? timeout, Encoding requestEncoding)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request= null;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback= new RemoteCertificateValidationCallback(CheckValidationResult);
request= WebRequest.Create(url) as HttpWebRequest;
request.Proxy= null;
request.Credentials= CredentialCache.DefaultCredentials;
request.ProtocolVersion= HttpVersion.Version10;
}
else
{
request= WebRequest.Create(url) as HttpWebRequest;
}
request.Method= "POST";
request.ContentType= "application/x-www-form-urlencoded";
if (timeout.HasValue)
{
request.Timeout= timeout.Value;
}
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 = requestEncoding.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data,0, data.Length);
}
}
return request.GetResponse() as HttpWebResponse;
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受 }
}
文章标题:Howtogethttpresponse.-创新互联
URL分享:http://myzitong.com/article/ceshjh.html