基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,284 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
|
||||
namespace Lskj.Util.HttpUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// Request请求帮助类
|
||||
/// </summary>
|
||||
public static class RequestUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:使用Get方法获取字符串结果(暂时没有加入Cookie)</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
public static string HttpGet(string url)
|
||||
{
|
||||
WebClient wc = new WebClient();
|
||||
wc.Encoding = Encoding.UTF8;
|
||||
return wc.DownloadString(url);
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:使用Get方法获取字符串结果(暂时没有加入Cookie)</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="header">The header.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
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);
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:使用Post方法获取字符串结果</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="cookieContainer">The cookie container.</param>
|
||||
/// <param name="fileName">Name of the file.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
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);
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:使用Post方法获取字符串结果</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="timeout">The timeout.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:使用Post方法获取字符串结果</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="header">The header.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:使用Post方法获取字符串结果</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="header">The header.</param>
|
||||
/// <param name="method">The method.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:使用Post方法获取字符串结果</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="cookieContainer">The cookie container.</param>
|
||||
/// <param name="fileStream">The file stream.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:请求是否发起自客户端的浏览器</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="httpContext">The HTTP context.</param>
|
||||
/// <returns><c>true</c> if [is sogou client request] [the specified HTTP context]; otherwise, <c>false</c>.</returns>
|
||||
public static bool IsSogouClientRequest(HttpContext httpContext)
|
||||
{
|
||||
return !string.IsNullOrEmpty(httpContext.Request.UserAgent) &&
|
||||
httpContext.Request.UserAgent.Contains("MicroMessenger");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user