基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Web.Script.Serialization;
|
||||
|
||||
namespace Lskj.Util.HttpUtil
|
||||
{
|
||||
public static class GetUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:获取Response Html</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-08-24 </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 Get(string url)
|
||||
{
|
||||
return RequestUtil.HttpGet(url);
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:获取JSON数据</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T GetJson<T>(string url)
|
||||
{
|
||||
string returnText = RequestUtil.HttpGet(url);
|
||||
JavaScriptSerializer js = new JavaScriptSerializer();
|
||||
T result = js.Deserialize<T>(returnText);
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:获取JSON数据</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="header">The header.</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T GetJson<T>(string url, WebHeaderCollection header)
|
||||
{
|
||||
string returnText = RequestUtil.HttpGet(url, header);
|
||||
JavaScriptSerializer js = new JavaScriptSerializer();
|
||||
T result = js.Deserialize<T>(returnText);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Lskj.Util.HttpUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// Request请求枚举
|
||||
/// </summary>
|
||||
public enum MethodEnum
|
||||
{
|
||||
POST,
|
||||
GET,
|
||||
PUT,
|
||||
DELETE
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Lskj.Util.HttpUtil
|
||||
{
|
||||
public sealed class NewEmp
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 同步数据
|
||||
/// </summary>
|
||||
/// <param name="
|
||||
/// ">没有特殊说明,传入</param>
|
||||
public static ResponseMode PostMethodToObj(string url, string jsonObj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return HttpUtil.PostUtil.PostUrl<ResponseMode>(url, jsonObj);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
//return JsonConvert.SerializeObject(new ResponseMode{ code = "-1", errorMsg = "非法操作." });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步数据
|
||||
/// </summary>
|
||||
public static ResponseMode GetUrl(string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
return HttpUtil.GetUtil.GetJson<ResponseMode>(url);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
//return JsonConvert.SerializeObject(new ResponseMode{ code = "-1", errorMsg = "非法操作." });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 新的响应对象
|
||||
/// </summary>
|
||||
public class ResponseMode
|
||||
{
|
||||
public int ret;
|
||||
public string msg;
|
||||
// public data data;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Web.Script.Serialization;
|
||||
|
||||
namespace Lskj.Util.HttpUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// Post帮助类
|
||||
/// </summary>
|
||||
public static class PostUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:获取Post结果</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="returnText">The return text.</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T GetResult<T>(string returnText)
|
||||
{
|
||||
JavaScriptSerializer js = new JavaScriptSerializer();
|
||||
T result = js.Deserialize<T>(returnText);
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:发起Post请求</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">返回数据类型(Json对应的实体)</typeparam>
|
||||
/// <param name="url">请求Url</param>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T PostUrl<T>(string url, string content)
|
||||
{
|
||||
string returnText = RequestUtil.HttpPost(url, content);
|
||||
var result = GetResult<T>(returnText);
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:发起Post请求</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="header">The header.</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T PostUrl<T>(string url, string content, WebHeaderCollection header)
|
||||
{
|
||||
string returnText = RequestUtil.HttpPost(url, content, header);
|
||||
var result = GetResult<T>(returnText);
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:发起Post请求</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <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>T.</returns>
|
||||
public static T PostUrl<T>(string url, string content, WebHeaderCollection header, MethodEnum method)
|
||||
{
|
||||
string returnText = RequestUtil.HttpPost(url, content, header, method);
|
||||
var result = GetResult<T>(returnText);
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:发起Post请求</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">返回数据类型(Json对应的实体)</typeparam>
|
||||
/// <param name="url">请求Url</param>
|
||||
/// <param name="cookieContainer">CookieContainer,如果不需要则设为null</param>
|
||||
/// <param name="fileName">要发送的文件名,如果不需要上传则设为null</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T PostGetJson<T>(string url, CookieContainer cookieContainer, string fileName)
|
||||
{
|
||||
string returnText = RequestUtil.HttpPost(url, cookieContainer, fileName);
|
||||
var result = GetResult<T>(returnText);
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:发起Post请求</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2019-07-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">返回数据类型(Json对应的实体)</typeparam>
|
||||
/// <param name="url">请求Url</param>
|
||||
/// <param name="cookieContainer">CookieContainer,如果不需要则设为null</param>
|
||||
/// <param name="fileStream">文件流</param>
|
||||
/// <returns>T.</returns>
|
||||
public static T PostGetJson<T>(string url, CookieContainer cookieContainer, Stream fileStream)
|
||||
{
|
||||
string returnText = RequestUtil.HttpPost(url, cookieContainer, fileStream);
|
||||
var result = GetResult<T>(returnText);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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