1、包装PostHttp请求
public string PPost(string url, string param, Method method, HtmlEncoding sendEncoding, HtmlEncoding receivEncoding, string referer, ref CookieContainer cookie, string contentType = "application/x-www-form-urlencoded")
{
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream stream = null;
StreamReader sr = null;
Stream reqStream = null;
string html = string.Empty;
try {
byte[] bs = Encoding.GetEncoding(sendEncoding.ToString() == "UTF8" ? "UTF-8" : sendEncoding.ToString()).GetBytes(param);
request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookie;
request.Method = method.ToString();
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.ContentLength = bs.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
request.ContentType = contentType;
request.Referer = referer;
request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*";
if (bs.Length > 0) {
reqStream = request.GetRequestStream();
reqStream.Write(bs, 0, bs.Length);
}
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
sr = new StreamReader(stream, Encoding.GetEncoding(receivEncoding.ToString() == "UTF8" ? "UTF-8" : receivEncoding.ToString()));
html = sr.ReadToEnd();
} catch {
html = "timeout";
} finally {
if (response != null) response.Close();
if (stream != null) stream.Dispose();
if (sr != null) sr.Dispose();
if (reqStream != null) reqStream.Dispose();
if (request != null) request.Abort();
}
return html;
}
2、签名
private void MakeSign(Dictionary<string, string> _param)
{
string paramStr = string.Join("", _param.Where(w => !string.IsNullOrEmpty(w.Value)).OrderBy(o => o.Key).Select(s => s.Key + s.Value).ToList<string>());
paramStr = string.Format("{0}{1}{0}", ServiceConfig.AppkeyDict[_param["client_id"]], paramStr);
string sign = EncodingHelper.MD5Encrypt(paramStr);
_param["sign"] = sign;
return _param;
}
public static string MD5Encrypt(string str)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(str));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < encryptedBytes.Length; i++)
{
sb.AppendFormat("{0:x2}", encryptedBytes[i]);
}
return sb.ToString().ToUpper();
}
3、暴露请求接口
public string XSRequest(Dictionary<string, string> _param) {
_param=this.MakeSign(_param);
string param = string.Join("&", _param.Where(w => !string.IsNullOrEmpty(w.Value)).Select(s => s.Key + "=" + s.Value).ToList<string>());
return collect.CollectHtml(_server_url, param, HttpHelper.Method.POST, HttpHelper.HtmlEncoding.UTF8, HttpHelper.HtmlEncoding.UTF8);
}
4、其中参数为字典,其中包含所有参数:公共参数和各个方法的私有参数都放里面
一般参数中都需要时间戳:
_param["timestamp"] = ConvertDateTimeInt(DateTime.Now).ToString();
public static long ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000000;
return t;
}