using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Net;
|
using System.Text;
|
using WIDESEA_Common.Tools;
|
using WIDESEA_Core.Utilities;
|
|
namespace WIDESEA_Core.Utilities
|
{
|
public class HttpHelper
|
{
|
//private static Logger logger = Logger.getLogger();
|
/// <summary>
|
/// 带参数的Post请求
|
/// </summary>
|
/// <param name="url"></param>
|
/// <param name="parameters"></param>
|
/// <returns></returns>
|
public static string PostByPara(string Url, string jsonParas)
|
{
|
//logger.DebugStart("HttpHelper.PostByPara");
|
//logger.DebugStart("Url=" + Url + ",jsonParas=" + jsonParas);
|
WriteLog.Info("PostByPara").Write("PostByPara接口调用开始" + Url + jsonParas + DateTime.Now, "PostByPara");
|
if (Url != string.Empty && jsonParas != string.Empty)
|
{
|
try
|
{
|
string strURL = Url;
|
HttpWebRequest request;
|
request = (HttpWebRequest)WebRequest.Create(strURL);
|
request.Method = "POST";
|
request.ContentType = "application/json;charset=UTF-8";
|
request.CookieContainer = null;
|
request.Timeout = 30000;
|
string paraUrlCoded = jsonParas;
|
byte[] payload;
|
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
|
request.ContentLength = payload.Length;
|
Stream writer = request.GetRequestStream();
|
writer.Write(payload, 0, payload.Length);
|
writer.Close();
|
System.Net.HttpWebResponse response;
|
response = (System.Net.HttpWebResponse)request.GetResponse();
|
System.IO.Stream s;
|
s = response.GetResponseStream();
|
string StrDate = "";
|
string strValue = "";
|
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
|
while ((StrDate = Reader.ReadLine()) != null)
|
{
|
strValue += StrDate + "\r\n";
|
}
|
Reader.Close();
|
response.Close();
|
//logger.DebugEnd("result=" + strValue);
|
//logger.DebugEnd("HttpHelper.PostByPara");
|
WriteLog.Info("PostByPara").Write("PostByPara接口调用结束" + Url + strValue + DateTime.Now, "PostByPara");
|
return strValue;
|
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Info("PostByPara").Write($"PostByPara接口调用失败:{Url},{ex.Message}", "PostByPara");
|
//logger.Error("HttpHelper.PostByPara -> " + ErrorCode.Common.cx100001 + " -> " + ErrorCode.Common.cx100001.GetDescription());
|
//logger.Error("HttpHelper.PostByPara -> " + ex.ToString());
|
}
|
}
|
else
|
{
|
//logger.Error("HttpHelper.PostByPara -> " + ErrorCode.Common.cx100002+ " -> " + ErrorCode.Common.cx100002.GetDescription());
|
|
}
|
//logger.DebugEnd("result=null");
|
//logger.DebugEnd("HttpHelper.PostByPara");
|
return null;
|
}
|
/// <summary>
|
/// 带参数的Get请求
|
/// </summary>
|
/// <param name="Url"></param>
|
/// <param name="param"></param>
|
/// <returns></returns>
|
public static string GetByPara(string Url, Dictionary<string, string> param)
|
{
|
//logger.DebugStart("HttpHelper.GetByPara");
|
//logger.DebugStart("Url=" + Url+ ",param.length="+ (param==null? "0":param.Count.ToString()));
|
if (Url != string.Empty)
|
{
|
try
|
{
|
StringBuilder builder = new StringBuilder();
|
builder.Append(Url);
|
if (param != null && param.Count > 0)
|
{
|
int i = 0;
|
foreach (var item in param)
|
{
|
if (i == 0)
|
{
|
builder.Append("?");
|
}
|
else
|
{
|
builder.Append("&");
|
}
|
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
i++;
|
}
|
}
|
string strURL = builder.ToString();
|
HttpWebRequest request;
|
request = (HttpWebRequest)WebRequest.Create(strURL);
|
request.Method = "GET";
|
request.Timeout = 30000;
|
System.Net.HttpWebResponse response;
|
response = (System.Net.HttpWebResponse)request.GetResponse();
|
System.IO.Stream s;
|
s = response.GetResponseStream();
|
string StrDate = "";
|
string strValue = "";
|
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
|
while ((StrDate = Reader.ReadLine()) != null)
|
{
|
strValue += StrDate + "\r\n";
|
}
|
Reader.Close();
|
response.Close();
|
//logger.DebugEnd("result=" + strValue);
|
//logger.DebugEnd("HttpHelper.GetByPara");
|
return strValue;
|
}
|
catch (Exception ex)
|
{
|
//logger.Error("HttpHelper.GetByPara -> " + ErrorCode.Common.cx100001 + " -> " + ErrorCode.Common.cx100001.GetDescription());
|
//logger.Error("HttpHelper.GetByPara -> " + ex.ToString());
|
}
|
}
|
else
|
{
|
//logger.Error("HttpHelper.GetByPara -> " + ErrorCode.Common.cx100002+ " -> " + ErrorCode.Common.cx100002.GetDescription());
|
}
|
//logger.DebugEnd("result=null");
|
//logger.DebugEnd("HttpHelper.GetByPara");
|
return null;
|
}
|
}
|
}
|