using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Net;
|
using System.Security.Cryptography;
|
using System.Text;
|
|
namespace HH.WCS.QingXigongchang.util
|
{
|
public class HttpHelper
|
{
|
public static string time = "";
|
public string _time
|
{
|
get
|
{
|
if (string.IsNullOrEmpty(time) || DateTime.Now.Subtract(Convert.ToDateTime(time)).TotalMinutes > 10)
|
time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
return time;
|
}
|
}
|
|
public static string GetMd5Hash(string input)
|
{
|
using (MD5 md5 = MD5.Create())
|
{
|
byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0; i < data.Length; i++)
|
{
|
sb.Append(data[i].ToString("x2"));
|
}
|
return sb.ToString();
|
}
|
}
|
|
public class PostStyle
|
{
|
public string url { get; set; }
|
public string postData { get; set; }
|
}
|
|
public string WebPost(string url, string postData, string cotentType = "application/json")
|
{
|
Console.WriteLine(url);
|
LogHelper.Info("WebPost" + url + ";;;" + postData);
|
WebRequest request = WebRequest.Create(url);
|
request.Method = "POST";
|
{
|
var sky = url.Contains(Settings.TaskReportUrl) ? Settings.TaskReportKey : Settings.securityKey;
|
|
string token = GetMd5Hash(sky + "from" + Settings.tokenfrom + "timestamp" + _time);
|
|
LogHelper.Info("header:" + JsonConvert.SerializeObject(new
|
{
|
token = token,
|
from = Settings.tokenfrom,
|
proxyInterfaceCode = Settings.OneSign,
|
timestamp = _time + "(十分钟有效期,所以10分钟内不会在生成。)"
|
}));
|
request.Headers.Add("token", token);
|
request.Headers.Add("from", Settings.tokenfrom);
|
request.Headers.Add("timestamp", _time);
|
request.Headers.Add("proxyInterfaceCode", Settings.OneSign);
|
}
|
//string postData = JsonConvert.SerializeObject(data); ;
|
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
|
request.ContentType = cotentType;
|
request.ContentLength = byteArray.Length;
|
request.Timeout = 6000;
|
|
try
|
{
|
Stream dataStream = request.GetRequestStream();
|
dataStream.Write(byteArray, 0, byteArray.Length);
|
dataStream.Close();
|
WebResponse response = request.GetResponse();
|
//Console.WriteLine(((HttpWebResponse)response).StatusDescription);
|
dataStream = response.GetResponseStream();
|
StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
|
string responseFromServer = reader.ReadToEnd();
|
reader.Close();
|
dataStream.Close();
|
response.Close();
|
LogHelper.Info("WebPost:responseFromServer" + responseFromServer);
|
return responseFromServer;
|
}
|
catch (Exception e)
|
{
|
//Console.WriteLine(e.Message);
|
LogHelper.Info("WebPost:e.Message" + e.Message);
|
return "";
|
}
|
}
|
|
public string WebGet(string url)
|
{
|
|
//using (var client = new HttpClient()) {
|
// //请求结果
|
// string result = client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
|
// Console.WriteLine(result);
|
// return result;
|
//}
|
//Console.WriteLine(url);
|
|
WebRequest request = WebRequest.Create(url);
|
request.Timeout = 6000;
|
request.Method = "GET";
|
|
try
|
{
|
WebResponse response = request.GetResponse();
|
Stream dataStream = response.GetResponseStream();
|
StreamReader reader = new StreamReader(dataStream);
|
string responseFromServer = reader.ReadToEnd();
|
|
reader.Close();
|
dataStream.Close();
|
response.Close();
|
Console.WriteLine(responseFromServer);
|
return responseFromServer;
|
}
|
catch (Exception e)
|
{
|
return "";
|
}
|
}
|
|
public string Get(string url, string contentType = "application/x-www-form-urlencoded")
|
{
|
WebRequest request = WebRequest.Create(url);
|
request.Method = "Get";
|
request.ContentType = contentType;
|
StreamReader reader = null;
|
Stream stream = null;
|
WebResponse rsp = null;
|
try
|
{
|
|
rsp = request.GetResponse();
|
stream = rsp.GetResponseStream();
|
reader = new StreamReader(stream);
|
return reader.ReadToEnd();
|
}
|
catch
|
{
|
return "";
|
}
|
finally
|
{
|
// 释放资源
|
if (reader != null) reader.Close();
|
if (stream != null) stream.Close();
|
if (rsp != null) rsp.Close();
|
}
|
|
}
|
|
public string Post(string url, string postData, string contentType = "application/json", string sessionId = "")
|
{
|
WebRequest request = WebRequest.Create(url);
|
request.Method = "POST";
|
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
|
request.ContentType = contentType;
|
request.ContentLength = byteArray.Length;
|
if (sessionId != "")
|
{
|
request.Headers.Set("ASP.NET_SessionId", sessionId);
|
}
|
StreamReader reader = null;
|
Stream stream = null;
|
WebResponse rsp = null;
|
try
|
{
|
stream = request.GetRequestStream();
|
stream.Write(byteArray, 0, byteArray.Length);
|
stream.Close();
|
rsp = request.GetResponse();
|
stream = rsp.GetResponseStream();
|
reader = new StreamReader(stream);
|
return reader.ReadToEnd();
|
}
|
catch
|
{
|
return "";
|
}
|
finally
|
{
|
// 释放资源
|
if (reader != null) reader.Close();
|
if (stream != null) stream.Close();
|
if (rsp != null) rsp.Close();
|
}
|
|
}
|
|
|
public string Post(string url, Dictionary<string, string> dic)
|
{
|
var param = dic.Select(a => { return string.Format("{0}={1}", a.Key, a.Value); }).ToList();
|
return Post(url, string.Join("&", param), "application/x-www-form-urlencoded");
|
|
}
|
|
public string PostWithCookie(string url, string sessinId)
|
{
|
return PostWithCookie(url, "", "", sessinId);
|
}
|
public string PostWithCookie(string url, string postData, string contentType = "application/json", string sessionId = "")
|
{
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
request.Method = "POST";
|
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
|
request.ContentType = contentType;
|
request.ContentLength = byteArray.Length;
|
if (sessionId != "")
|
{
|
request.CookieContainer = new CookieContainer();
|
request.CookieContainer.SetCookies(new Uri("http://" + request.RequestUri.Authority), "ASP.NET_SessionId=" + sessionId);
|
}
|
StreamReader reader = null;
|
Stream stream = null;
|
WebResponse rsp = null;
|
try
|
{
|
stream = request.GetRequestStream();
|
stream.Write(byteArray, 0, byteArray.Length);
|
stream.Close();
|
rsp = request.GetResponse();
|
stream = rsp.GetResponseStream();
|
reader = new StreamReader(stream);
|
return reader.ReadToEnd();
|
}
|
catch
|
{
|
return "";
|
}
|
finally
|
{
|
// 释放资源
|
if (reader != null) reader.Close();
|
if (stream != null) stream.Close();
|
if (rsp != null) rsp.Close();
|
}
|
|
}
|
|
|
}
|
}
|