|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
/**
|
|
|
|
|
* 创建人: robin
|
|
|
|
|
* */
|
|
|
|
|
namespace Admin.Core.Common
|
|
|
|
|
{
|
|
|
|
|
public class HttpHelper
|
|
|
|
|
{
|
|
|
|
|
public static string Get(string serviceAddress)
|
|
|
|
|
{
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
|
|
|
|
|
request.Method = "GET";
|
|
|
|
|
request.ContentType = "text/html;charset=UTF-8";
|
|
|
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
|
|
Stream myResponseStream = response.GetResponseStream();
|
|
|
|
|
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
|
|
|
|
|
string retString = myStreamReader.ReadToEnd();
|
|
|
|
|
myStreamReader.Close();
|
|
|
|
|
myResponseStream.Close();
|
|
|
|
|
return retString;
|
|
|
|
|
}
|
|
|
|
|
public static async Task<string> GetAsync(string serviceAddress)
|
|
|
|
|
{
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
|
|
|
|
|
request.Method = "GET";
|
|
|
|
|
request.ContentType = "text/html;charset=UTF-8";
|
|
|
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
|
|
Stream myResponseStream = response.GetResponseStream();
|
|
|
|
|
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
|
|
|
|
|
string retString = await myStreamReader.ReadToEndAsync();
|
|
|
|
|
myStreamReader.Close();
|
|
|
|
|
myResponseStream.Close();
|
|
|
|
|
return retString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Post(string serviceAddress, string strContent = null)
|
|
|
|
|
{
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
|
|
|
|
|
request.Method = "POST";
|
|
|
|
|
request.ContentType = "application/json";
|
|
|
|
|
//判断有无POST内容
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(strContent))
|
|
|
|
|
{
|
|
|
|
|
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
|
|
|
|
|
{
|
|
|
|
|
dataStream.Write(strContent);
|
|
|
|
|
dataStream.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
|
|
string encoding = response.ContentEncoding;
|
|
|
|
|
if (encoding.Length < 1)
|
|
|
|
|
{
|
|
|
|
|
encoding = "UTF-8"; //默认编码
|
|
|
|
|
}
|
|
|
|
|
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
|
|
|
|
|
string retString = reader.ReadToEnd();
|
|
|
|
|
return retString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<string> PostAsync(string serviceAddress, string strContent = null)
|
|
|
|
|
{
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
|
|
|
|
|
request.Method = "POST";
|
|
|
|
|
request.ContentType = "application/json";
|
|
|
|
|
//判断有无POST内容
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(strContent))
|
|
|
|
|
{
|
|
|
|
|
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
|
|
|
|
|
{
|
|
|
|
|
dataStream.Write(strContent);
|
|
|
|
|
dataStream.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
|
|
string encoding = response.ContentEncoding;
|
|
|
|
|
if (encoding.Length < 1)
|
|
|
|
|
{
|
|
|
|
|
encoding = "UTF-8"; //默认编码
|
|
|
|
|
}
|
|
|
|
|
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
|
|
|
|
|
string retString = await reader.ReadToEndAsync();
|
|
|
|
|
return retString;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|