using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Net;
|
using System.Text;
|
|
namespace HH.WCS.Mobox3.AnGang.util {
|
public class HttpHelper {
|
public string WebPost(string url, string postData, string cotentType = "application/json")
|
{
|
Console.WriteLine(url);
|
WebRequest request = WebRequest.Create(url);
|
request.Method = "POST";
|
//string postData = JsonConvert.SerializeObject(data); ;
|
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
|
request.ContentType = cotentType;
|
request.ContentLength = byteArray.Length;
|
request.Timeout = 3000;
|
StreamReader reader = null;
|
Stream dataStream = null;
|
WebResponse response = null;
|
|
try
|
{
|
dataStream = request.GetRequestStream();
|
dataStream.Write(byteArray, 0, byteArray.Length);
|
dataStream.Close();
|
response = request.GetResponse();
|
//Console.WriteLine(((HttpWebResponse)response).StatusDescription);
|
dataStream = response.GetResponseStream();
|
reader = new StreamReader(dataStream, Encoding.UTF8);
|
string responseFromServer = reader.ReadToEnd();
|
return responseFromServer;
|
}
|
catch (Exception e)
|
{
|
LogHelper.Info($"WebPost res={e.Message}", "API");
|
return "";
|
}
|
finally
|
{
|
// 释放资源
|
if (reader != null) reader.Close();
|
if (dataStream != null) dataStream.Close();
|
if (response != null) response.Close();
|
}
|
}
|
|
public string WebGet(string url) {
|
WebRequest request = WebRequest.Create(url);
|
request.Timeout = 6000;
|
request.Method = "GET";
|
StreamReader reader = null;
|
Stream dataStream = null;
|
WebResponse response = null;
|
try {
|
response = request.GetResponse();
|
dataStream = response.GetResponseStream();
|
reader = new StreamReader(dataStream);
|
string responseFromServer = reader.ReadToEnd();
|
|
Console.WriteLine(responseFromServer);
|
return responseFromServer;
|
}
|
catch (Exception e) {
|
LogHelper.Info($"WebGet res={e.Message}", "API");
|
return "";
|
}
|
finally
|
{
|
// 释放资源
|
if (reader != null) reader.Close();
|
if (dataStream != null) dataStream.Close();
|
if (response != null) response.Close();
|
}
|
}
|
|
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 (Exception e)
|
{
|
LogHelper.Info($"Get res={e.Message}", "API");
|
return "";
|
}
|
finally {
|
// 释放资源
|
if (reader != null) reader.Close();
|
if (stream != null) stream.Close();
|
if (rsp != null) rsp.Close();
|
}
|
}
|
|
public string GetNew(string url, string contentType = null) {
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
request.Method = "GET";
|
|
// 只有指定了contentType时才设置
|
if (!string.IsNullOrEmpty(contentType)) {
|
request.ContentType = contentType;
|
}
|
|
try {
|
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
using (Stream stream = response.GetResponseStream())
|
using (StreamReader reader = new StreamReader(stream)) {
|
return reader.ReadToEnd();
|
}
|
}
|
catch (WebException ex) when (ex.Response is HttpWebResponse response) {
|
// 记录详细的错误信息
|
LogHelper.Info($"GET请求失败。状态码: {response.StatusCode}, URL: {url}", "API");
|
|
// 读取错误响应内容
|
using (Stream stream = response.GetResponseStream())
|
using (StreamReader reader = new StreamReader(stream)) {
|
string errorResponse = reader.ReadToEnd();
|
LogHelper.Info($"错误响应内容: {errorResponse}", "API");
|
}
|
return string.Empty;
|
}
|
catch (Exception ex) {
|
LogHelper.Info($"GET请求失败。URL: {url}, 错误: {ex.Message}", "API");
|
return string.Empty;
|
}
|
}
|
|
public string Post(string url, string postData, string contentType = "application/json", string sessionId = "") {
|
Console.WriteLine(url);
|
WebRequest request = WebRequest.Create(url);
|
request.Method = "POST";
|
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
|
request.ContentType = contentType;
|
request.ContentLength = byteArray.Length;
|
request.Timeout = 3000;
|
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 (Exception ex) {
|
LogHelper.Info($"Post res={ex.Message}", "API");
|
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 (Exception e)
|
{
|
|
LogHelper.Info($"PostWithCookie res={e.Message}", "API");
|
return "";
|
}
|
finally {
|
// 释放资源
|
if (reader != null) reader.Close();
|
if (stream != null) stream.Close();
|
if (rsp != null) rsp.Close();
|
}
|
|
}
|
|
public string Put(string url, string postData, string contentType = "application/json", string sessionId = "")
|
{
|
WebRequest request = WebRequest.Create(url);
|
request.Method = "Put";
|
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 (Exception e)
|
{
|
LogHelper.Info($"Put res={e.Message}", "API");
|
return "";
|
}
|
finally
|
{
|
// 释放资源
|
if (reader != null) reader.Close();
|
if (stream != null) stream.Close();
|
if (rsp != null) rsp.Close();
|
}
|
}
|
|
public string WebPostWithMD5(string url, string postData, string contentType = "application/json")
|
{
|
// 新增授权参数
|
string appKey = ""; // 假设配置类中有AppKey和AppSecret
|
string appSecret = "";
|
string reqTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
|
string reqVerify = CalculateMD5Hash($"{appKey}{appSecret}{reqTime}");
|
|
WebRequest request = WebRequest.Create(url);
|
request.Method = "POST";
|
request.Headers.Add("AppKey", appKey);
|
request.Headers.Add("ReqTime", reqTime);
|
request.Headers.Add("ReqVerify", reqVerify);
|
|
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
|
request.ContentType = contentType;
|
request.ContentLength = byteArray.Length;
|
request.Timeout = 3000;
|
StreamReader reader = null;
|
Stream dataStream = null;
|
WebResponse response = null;
|
|
try
|
{
|
dataStream = request.GetRequestStream();
|
dataStream.Write(byteArray, 0, byteArray.Length);
|
dataStream.Close();
|
response = request.GetResponse();
|
//Console.WriteLine(((HttpWebResponse)response).StatusDescription);
|
dataStream = response.GetResponseStream();
|
reader = new StreamReader(dataStream, Encoding.UTF8);
|
string responseFromServer = reader.ReadToEnd();
|
return responseFromServer;
|
}
|
catch (Exception e)
|
{
|
LogHelper.Info($"WebPost res={e.Message}", "API");
|
return "";
|
}
|
finally
|
{
|
// 释放资源
|
if (reader != null) reader.Close();
|
if (dataStream != null) dataStream.Close();
|
if (response != null) response.Close();
|
}
|
}
|
|
// MD5计算辅助方法
|
private static string CalculateMD5Hash(string input)
|
{
|
using (var md5 = System.Security.Cryptography.MD5.Create())
|
{
|
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
|
byte[] hashBytes = md5.ComputeHash(inputBytes);
|
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
|
}
|
}
|
}
|
}
|