67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Text;
|
|
|
|
namespace CRM.Core.Common
|
|
{
|
|
public static class Utility
|
|
{
|
|
public static string GetData(string Url, string RequestPara)
|
|
{
|
|
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.UTF8);
|
|
string ReturnVal = reader.ReadToEnd();
|
|
reader.Close();
|
|
response.Close();
|
|
|
|
return ReturnVal;
|
|
}
|
|
/// <summary>
|
|
/// 创建POST方式的HTTP请求
|
|
/// </summary>
|
|
/// <param name="url">请求的URL</param>
|
|
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
|
|
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
|
|
/// <returns></returns>
|
|
public static string HttpPostData(string url, string parameters, Encoding requestEncoding)
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
throw new ArgumentNullException("url");
|
|
}
|
|
if (requestEncoding == null)
|
|
{
|
|
throw new ArgumentNullException("requestEncoding");
|
|
}
|
|
HttpWebRequest request = null;
|
|
request = WebRequest.Create(url) as HttpWebRequest;
|
|
request.Method = "POST";
|
|
request.ContentType = "application/x-www-form-urlencoded";
|
|
// request.ContentType = "application/json";
|
|
//如果需要POST数据
|
|
if (!string.IsNullOrEmpty(parameters))
|
|
{
|
|
byte[] data = requestEncoding.GetBytes(parameters.ToString());
|
|
using (Stream stream = request.GetRequestStream())
|
|
{
|
|
stream.Write(data, 0, data.Length);
|
|
}
|
|
}
|
|
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
|
{
|
|
StreamReader reader = new StreamReader(response.GetResponseStream(), requestEncoding);
|
|
string result = reader.ReadToEnd();
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|