Files
lserp_cs_6.0/插件库/Lskj.Util/HttpHelper.cs
T
tdx 02379d3c01 SVN r942
SVN-Revision: r942
2025-08-11 08:32:32 +00:00

201 lines
8.1 KiB
C#

using Lskj.Web.Core.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace Lskj.Util
{
public static class HttpHelper
{
public enum Encode
{
Default = 0,
UTF8 = 1
}
private static string ContentType = string.Empty;// "application/x-www-form-urlencoded";
private static string Accept = string.Empty;//"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
private static string UserAgent = string.Empty;//"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14";
private static Encode EncodeMode = Encode.Default;
public static void Setting(string contentType, string accept, string userAgent, Encode encode = Encode.Default)
{
ContentType = contentType;
Accept = accept;
UserAgent = userAgent;
EncodeMode = encode;
}
/// <summary>
/// post数据到指定的网址,获取cookie数据,和返回页
/// </summary>
public static HttpWebResponse Post(string url, string postData, Dictionary<string, string> pmsDic, Dictionary<string, string> headerDic, out string result)
{
HttpWebResponse httpWebResponse = null;
try
{
HttpWebRequest httpWebRequest;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.ContentType = ContentType;
httpWebRequest.Accept = Accept;
httpWebRequest.UserAgent = UserAgent;
httpWebRequest.Method = "POST";
byte[] byteRequest = null;
if (headerDic != null)
{
foreach (var item in headerDic)
{
if (item.Key.Equals("Content-Type"))
{
httpWebRequest.ContentType = item.Value;
}
else
{
httpWebRequest.Headers.Add(item.Key, item.Value);
}
}
}
if (!string.IsNullOrEmpty(postData))
{
switch (EncodeMode)
{
case Encode.Default:
byteRequest = Encoding.Default.GetBytes(postData);
break;
case Encode.UTF8:
byteRequest = Encoding.UTF8.GetBytes(postData);
break;
}
httpWebRequest.ContentLength = byteRequest.Length;
}
else if (pmsDic != null)
{
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in pmsDic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
switch (EncodeMode)
{
case Encode.Default:
byteRequest = Encoding.Default.GetBytes(builder.ToString());
break;
case Encode.UTF8:
byteRequest = Encoding.UTF8.GetBytes(builder.ToString());
break;
}
httpWebRequest.ContentLength = byteRequest.Length;
}
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
result = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
return httpWebResponse;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
result = ex.Message;
return httpWebResponse;
}
}
/// <summary>
/// Get到指定的网址,获取cookie数据,和返回页
/// </summary>
public static HttpWebResponse Get(string url, string postData, Dictionary<string, string> pmsDic, Dictionary<string, string> headerDic, out string result)
{
HttpWebResponse httpWebResponse = null;
try
{
StringBuilder builder = new StringBuilder(url);
if (pmsDic != null && pmsDic.Count > 0)
{
builder.Append("?");
int i = 0;
foreach (var item in pmsDic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
}
HttpWebRequest httpWebRequest;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(builder.ToString());
httpWebRequest.ContentType = ContentType;
//httpWebRequest.Referer = url;
httpWebRequest.Accept = Accept;
httpWebRequest.UserAgent = UserAgent;
httpWebRequest.Method = "GET";
if (headerDic != null)
{
foreach (var item in headerDic)
{
if (item.Key.Equals("Content-Type"))
{
httpWebRequest.ContentType = item.Value;
}
else
{
httpWebRequest.Headers.Add(item.Key, item.Value);
}
}
}
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
result = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
return httpWebResponse;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
result = ex.Message;
return httpWebResponse;
}
}
/// <summary>
/// url转换
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string ToEnUrl(string url)
{
string[] urlParams = url.Split('?');
string enKey = "encrypt", enVal = "";
bool hasEn = false;
if (urlParams.Length > 1)
{
string[] queryParams = urlParams[1].Split('&');
List<string> qpLS = new List<string>();
Hashtable pms = new Hashtable();
hasEn = queryParams.Any(str => str.Trim().StartsWith(enKey, StringComparison.OrdinalIgnoreCase));
foreach (string q in queryParams)
{
string[] eqParams = q.Split('=');
if (eqParams.Length > 1)
{
if (hasEn && eqParams[0].ToLower() == enKey)
{
enVal = eqParams[1];
continue;
}
if (hasEn || eqParams[0].ToLower() == "username" || eqParams[0].ToLower() == "password")
{
pms.Add(eqParams[0],