using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace SynchronousOven.HttpUtil
{
///
/// Request请求帮助类
///
public static class RequestUtil
{
///
/// 说明:使用Get方法获取字符串结果(暂时没有加入Cookie)
/// 创建人:龚宇超
/// 创建日期:2019-07-30
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The URL.
/// System.String.
public static string HttpGet(string url)
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
return wc.DownloadString(url);
}
///
/// 说明:使用Get方法获取字符串结果(暂时没有加入Cookie)
/// 创建人:龚宇超
/// 创建日期:2019-07-30
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The URL.
/// The header.
/// System.String.
public static string HttpGet(string url, WebHeaderCollection header)
{
WebClient wc = new WebClient();
if (header != null)
{
wc.Headers = header;
}
wc.Encoding = Encoding.UTF8;
return wc.DownloadString(url);
}
///
/// 说明:使用Post方法获取字符串结果
/// 创建人:龚宇超
/// 创建日期:2019-07-30
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The URL.
/// The cookie container.
/// Name of the file.
/// System.String.
public static string HttpPost(string url, CookieContainer cookieContainer, string fileName)
{
FileStream fileStream = null;
if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
{
//读取文件
fileStream = new FileStream(fileName, FileMode.Open);
}
return HttpPost(url, cookieContainer, fileStream);
}
///
/// 说明:使用Post方法获取字符串结果
/// 创建人:龚宇超
/// 创建日期:2019-07-30
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The URL.
/// The content.
/// The timeout.
/// System.String.
public static string HttpPost(string url, string content, int timeout = 5000)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/json;charset=utf8";
request.Timeout = timeout;
byte[] buffer = Encoding.UTF8.GetBytes(content);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
Console.WriteLine(ex.Message);
return "";
}
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
{
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
}
///
/// 说明:使用Post方法获取字符串结果
/// 创建人:龚宇超
/// 创建日期:2019-07-30
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The URL.
/// The content.
/// The header.
/// System.String.
public static string HttpPost(string url, string content, WebHeaderCollection header)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
if (header != null)
{
request.Headers = header;
}
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/json;charset=utf8";
byte[] buffer = Encoding.UTF8.GetBytes(content);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
Console.WriteLine(ex.Message);
return "";
}
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
{
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
}
///
/// 说明:使用Post方法获取字符串结果
/// 创建人:龚宇超
/// 创建日期:2019-07-30
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The URL.
/// The content.
/// The header.
/// The method.
/// System.String.
public static string HttpPost(string url, string content, WebHeaderCollection header, MethodEnum method)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method.ToString();
if (header != null)
{
request.Headers = header;
}
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/json;charset=utf8";
byte[] buffer = Encoding.UTF8.GetBytes(content);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
Console.WriteLine(ex.Message);
return "";
}
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
{
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
}
///
/// 说明:使用Post方法获取字符串结果
/// 创建人:龚宇超
/// 创建日期:2019-07-30
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The URL.
/// The cookie container.
/// The file stream.
/// System.String.
public static string HttpPost(string url, CookieContainer cookieContainer, Stream fileStream)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentType = "application/json;charset=utf8";
request.ContentLength = fileStream != null ? fileStream.Length : 0;
if (cookieContainer != null)
{
request.CookieContainer = cookieContainer;
}
if (fileStream != null)
{
Stream requestStream = request.GetRequestStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();//关闭文件访问
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (cookieContainer != null)
{
response.Cookies = cookieContainer.GetCookies(response.ResponseUri);
}
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
{
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
}
///
/// 说明:请求是否发起自客户端的浏览器
/// 创建人:龚宇超
/// 创建日期:2019-07-30
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The HTTP context.
/// true if [is sogou client request] [the specified HTTP context]; otherwise, false.
public static bool IsSogouClientRequest(HttpContext httpContext)
{
return !string.IsNullOrEmpty(httpContext.Request.UserAgent) &&
httpContext.Request.UserAgent.Contains("MicroMessenger");
}
}
}