123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Web;
- using System.Net.Security;
- using System.Security.Authentication;
- using System.Security.Cryptography.X509Certificates;
- namespace cuidian.OpenApi.Http
- {
- public sealed class WebUtils
- {
- private int _timeout = 100000;
-
-
-
- public int Timeout
- {
- get { return this._timeout; }
- set { this._timeout = value; }
- }
-
-
-
-
-
-
- public HttpWebResponse Post(string url, IDictionary<string, string> parameters, string data)
- {
- if (parameters != null && parameters.Count > 0)
- {
- if (url.Contains("?"))
- {
- url = url + "&" + BuildQuery(parameters);
- }
- else
- {
- url = url + "?" + BuildQuery(parameters);
- }
- }
- HttpWebRequest req = GetWebRequest(url, "POST");
- req.ContentType = "application/json;charset=utf-8";
- byte[] postData = Encoding.UTF8.GetBytes(data);
- System.IO.Stream reqStream = req.GetRequestStream();
- reqStream.Write(postData, 0, postData.Length);
- reqStream.Close();
- return (HttpWebResponse)req.GetResponse();
- }
-
-
-
-
-
-
- public HttpWebResponse Get(string url, IDictionary<string, string> parameters)
- {
- if (parameters != null && parameters.Count > 0)
- {
- if (url.Contains("?"))
- {
- url = url + "&" + BuildQuery(parameters);
- }
- else
- {
- url = url + "?" + BuildQuery(parameters);
- }
- }
- Console.WriteLine("url:" + url);
- HttpWebRequest req = GetWebRequest(url, "GET");
- req.ContentType = "application/json;charset=utf-8";
- return (HttpWebResponse)req.GetResponse();
- }
- public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
- {
- return true;
- }
- public HttpWebRequest GetWebRequest(string url, string method)
- {
- HttpWebRequest req = null;
- if (url.Contains("https"))
- {
- ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
- req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
- }
- else
- {
- req = (HttpWebRequest)WebRequest.Create(url);
- }
- req.ServicePoint.Expect100Continue = false;
- req.Method = method;
- req.KeepAlive = true;
- req.UserAgent = "openapi4net:open.yonyouup.com";
- req.Timeout = this._timeout;
- return req;
- }
-
-
-
-
-
-
- public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
- {
- System.IO.Stream stream = null;
- StreamReader reader = null;
- try
- {
-
- stream = rsp.GetResponseStream();
- reader = new StreamReader(stream, encoding);
- return reader.ReadToEnd();
- }
- finally
- {
-
- if (reader != null) reader.Close();
- if (stream != null) stream.Close();
- if (rsp != null) rsp.Close();
- }
- }
-
-
-
-
-
-
- public string BuildGetUrl(string url, IDictionary<string, string> parameters)
- {
- if (parameters != null && parameters.Count > 0)
- {
- if (url.Contains("?"))
- {
- url = url + "&" + BuildQuery(parameters);
- }
- else
- {
- url = url + "?" + BuildQuery(parameters);
- }
- }
- return url;
- }
-
-
-
-
-
- public static string BuildQuery(IDictionary<string, string> parameters)
- {
- StringBuilder postData = new StringBuilder();
- bool hasParam = false;
- IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
- while (dem.MoveNext())
- {
- string name = dem.Current.Key;
- string value = dem.Current.Value;
-
- if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
- {
- if (hasParam)
- {
- postData.Append("&");
- }
- postData.Append(name);
- postData.Append("=");
- postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
- hasParam = true;
- }
- }
- return postData.ToString();
- }
- }
- }
|