海门当升调度软件
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.1 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace CommonHelper
{
public static class HttpHelper
{
/// <summary>
/// Get获取数据
/// </summary>
/// <param name="getUrl"></param>
/// <param name="resultData"></param>
/// <returns></returns>
public static bool GetData(string getUrl, ref string resultData)
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
request.Method = "GET";
using (HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse())
{
using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), encoding))
{
resultData = sr.ReadToEnd();
}
}
return true;
}
/// <summary>
/// Post提交数据
/// </summary>
/// <param name="postUrl">Url</param>
/// <param name="postData">提交数据</param>
/// <param name="resultData">结果数据</param>
/// <returns></returns>
public static bool PostData(string postUrl, string postData, ref string resultData)
{
return PostData(postUrl, postData, "application/json;charset=utf-8", null, ref resultData);
}
/// <summary>
/// Post提交数据
/// </summary>
/// <param name="postUrl">Url</param>
/// <param name="postData">提交数据</param>
/// <param name="headers">HTTP标头键值对</param>
/// <param name="resultData">结果数据</param>
/// <returns></returns>
public static bool PostData(string postUrl, string postData, Dictionary<string, string> headers, ref string resultData)
{
return PostData(postUrl, postData, "application/json;charset=utf-8", headers, ref resultData);
}
/// <summary>
/// Post提交数据
/// </summary>
/// <param name="postUrl">Url</param>
/// <param name="postData">提交数据</param>
/// <param name="contentType">Content-type HTTP 标头的值</param>
/// <param name="resultData">结果数据</param>
/// <returns></returns>
public static bool PostData(string postUrl, string postData, string contentType, ref string resultData)
{
return PostData(postUrl, postData, contentType, null, ref resultData);
}
/// <summary>
/// Post提交数据
/// </summary>
/// <param name="postUrl">Url</param>
/// <param name="postData">提交数据</param>
/// <param name="contentType">Content-type HTTP 标头的值</param>
/// <param name="headers">HTTP标头键值对</param>
/// <param name="resultData">结果数据</param>
/// <returns></returns>
public static bool PostData(string postUrl, string postData, string contentType, Dictionary<string, string> headers, ref string resultData)
{
Encoding encoding = Encoding.UTF8;
byte[] dataByte = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
request.Method = "POST";
request.ContentType = contentType;
request.ContentLength = dataByte.Length;
if (headers != null)
{
foreach (var obj in headers)
{
request.Headers.Add(obj.Key, obj.Value);
}
}
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(dataByte, 0, dataByte.Length);
}
using (HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse())
{
using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), encoding))
{
resultData = sr.ReadToEnd();
}
}
return true;
}
}
}