101 lines
3.7 KiB
C#
101 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DG.Tool
|
|
{
|
|
public static class HttpHelper
|
|
{
|
|
public static string PostAjaxData(string url, string param, Encoding encoding)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
request.Method = "POST";
|
|
request.ContentType = "application/json;charet=utf-8";
|
|
request.Headers.Add("dataType", "json");
|
|
request.Headers.Add("type", "post");
|
|
byte[] data = encoding.GetBytes(param);
|
|
|
|
using (BinaryWriter reqStream = new BinaryWriter(request.GetRequestStream()))
|
|
{
|
|
reqStream.Write(data, 0, data.Length);
|
|
}
|
|
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
|
{
|
|
StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
|
|
string result = reader.ReadToEnd();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public static T PostAjaxData<T>(string url, string param, Encoding encoding)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
request.Method = "POST";
|
|
request.ContentType = "application/json;charet=utf-8";
|
|
request.Headers.Add("dataType", "json");
|
|
request.Headers.Add("type", "post");
|
|
byte[] data = encoding.GetBytes(param);
|
|
|
|
using (BinaryWriter reqStream = new BinaryWriter(request.GetRequestStream()))
|
|
{
|
|
reqStream.Write(data, 0, data.Length);
|
|
}
|
|
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
|
{
|
|
StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
|
|
string result = reader.ReadToEnd();
|
|
return JsonHelper.FromJson<T>(result);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取数据
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="encoding"></param>
|
|
/// <returns></returns>
|
|
public static string GetData(string Url, string RequestPara, Encoding encoding)
|
|
{
|
|
RequestPara = RequestPara.IndexOf('?') > -1 ? (RequestPara) : ("?" + RequestPara);
|
|
|
|
WebRequest hr = HttpWebRequest.Create(Url + RequestPara);
|
|
|
|
byte[] buf = encoding.GetBytes(RequestPara);
|
|
hr.Method = "GET";
|
|
|
|
System.Net.WebResponse response = hr.GetResponse();
|
|
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
|
|
string ReturnVal = reader.ReadToEnd();
|
|
reader.Close();
|
|
response.Close();
|
|
|
|
return ReturnVal;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取数据
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="encoding"></param>
|
|
/// <returns></returns>
|
|
public static T GetData<T>(string Url, string RequestPara, Encoding encoding)
|
|
{
|
|
RequestPara = RequestPara.IndexOf('?') > -1 ? (RequestPara) : ("?" + RequestPara);
|
|
|
|
WebRequest hr = HttpWebRequest.Create(Url + RequestPara);
|
|
|
|
byte[] buf = encoding.GetBytes(RequestPara);
|
|
hr.Method = "GET";
|
|
|
|
System.Net.WebResponse response = hr.GetResponse();
|
|
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
|
|
string ReturnVal = reader.ReadToEnd();
|
|
reader.Close();
|
|
response.Close();
|
|
return JsonHelper.FromJson<T>(ReturnVal);
|
|
}
|
|
}
|
|
} |