You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
6.9 KiB
180 lines
6.9 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AIK.Common.WebHelper
|
|
{
|
|
public class WebHelper
|
|
{
|
|
|
|
//private static string weburlTest = "https://keytest.aik518.com";
|
|
////public static string fileurl = "https://keytest.aik518.com/prod-api/work_file/file/file_download?objectName=";
|
|
////接口地址
|
|
//private static string weburl = " https://key.aik518.com";
|
|
////文件下载地址
|
|
//public static string fileurl = " https://key.aik518.com/prod-api/work_file/file/file_download?objectName=";
|
|
////当前平台语言
|
|
//public static string language = "zh";
|
|
////当前平台升级地址
|
|
//private static string updateurl = "http://124.221.137.201:8168/update.zip";
|
|
|
|
//public static System.Collections.Hashtable ImgListKey = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
|
|
|
|
|
|
|
|
#region HttpWebRequest(请求网络资源)
|
|
|
|
/// <summary>
|
|
/// 请求网络资源,返回响应的文本
|
|
/// </summary>
|
|
/// <param name="url">网络资源地址</param>
|
|
public static string HttpWebRequest(string url, string parameters, int timeout, bool isPost)
|
|
{
|
|
return HttpWebRequest(url, parameters, Encoding.GetEncoding("utf-8"), timeout, isPost);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 请求网络资源,返回响应的文本
|
|
/// </summary>
|
|
/// <param name="url">网络资源地址</param>
|
|
/// <param name="parameters">提交的参数,格式:参数1=参数值1&参数2=参数值2</param>
|
|
/// <param name="encoding">字符编码</param>
|
|
/// <param name="isPost">是否Post提交</param>
|
|
/// <param name="contentType">内容类型</param>
|
|
/// <param name="cookie">Cookie容器</param>
|
|
/// <param name="timeout">超时时间</param>
|
|
public static string HttpWebRequest(string url, string parameters, Encoding encoding, int timeout, bool isPost = false,
|
|
string contentType = "application/x-www-form-urlencoded", CookieContainer cookie = null)
|
|
{
|
|
var request = (HttpWebRequest)WebRequest.Create(url);
|
|
request.Timeout = timeout;
|
|
request.CookieContainer = cookie;
|
|
if (isPost)
|
|
{
|
|
byte[] postData = encoding.GetBytes(parameters);
|
|
request.Method = "POST";
|
|
request.ContentType = contentType;
|
|
request.ContentLength = postData.Length;
|
|
using (Stream stream = request.GetRequestStream())
|
|
{
|
|
stream.Write(postData, 0, postData.Length);
|
|
}
|
|
}
|
|
var response = (HttpWebResponse)request.GetResponse();
|
|
string result;
|
|
using (Stream stream = response.GetResponseStream())
|
|
{
|
|
if (stream == null)
|
|
return string.Empty;
|
|
using (var reader = new StreamReader(stream, encoding))
|
|
{
|
|
result = reader.ReadToEnd();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Post json请求
|
|
public static string PostJson(string Url, string jsonParas)
|
|
{
|
|
string strURL = Url;
|
|
//创建一个HTTP请求
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
|
|
//Post请求方式
|
|
request.Method = "POST";
|
|
//内容类型
|
|
request.ContentType = "application/json";
|
|
//设置参数,并进行URL编码
|
|
string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas);
|
|
byte[] payload;
|
|
//将Json字符串转化为字节
|
|
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
|
|
//设置请求的ContentLength
|
|
request.ContentLength = payload.Length;
|
|
//发送请求,获得请求流
|
|
Stream writer;
|
|
try
|
|
{
|
|
using (writer = request.GetRequestStream())
|
|
{
|
|
//将请求参数写入流
|
|
writer.Write(payload, 0, payload.Length);
|
|
writer.Close();//关闭请求流
|
|
}
|
|
;//获取用于写入请求数据的Stream对象
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
writer = null;
|
|
}
|
|
|
|
HttpWebResponse response;
|
|
string postContent = "";
|
|
try
|
|
{
|
|
//获得响应流
|
|
using (response = (HttpWebResponse)request.GetResponse())
|
|
{
|
|
Stream s = response.GetResponseStream();
|
|
// Stream postData = Request.InputStream;
|
|
StreamReader sRead = new StreamReader(s);
|
|
postContent = sRead.ReadToEnd();
|
|
sRead.Close();
|
|
}
|
|
;
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
response = ex.Response as HttpWebResponse;
|
|
}
|
|
return postContent;//返回Json数据
|
|
}
|
|
#endregion
|
|
|
|
#region 异步请求
|
|
/// <summary>
|
|
/// 异步POST请求
|
|
/// </summary>
|
|
public static async Task<string> PostJsonAsync(string url, string jsonData, CancellationToken cancellationToken = default)
|
|
{
|
|
using var client = new HttpClient();
|
|
using var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
|
|
using var response = await client.PostAsync(url, content, cancellationToken);
|
|
response.EnsureSuccessStatusCode();
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步HTTP请求
|
|
/// </summary>
|
|
public static async Task<string> HttpWebRequestAsync(string url, string data, int timeoutMs, bool isPost, CancellationToken cancellationToken = default)
|
|
{
|
|
using var client = new HttpClient();
|
|
client.Timeout = TimeSpan.FromMilliseconds(timeoutMs);
|
|
|
|
if (isPost && !string.IsNullOrEmpty(data))
|
|
{
|
|
using var content = new StringContent(data, Encoding.UTF8, "application/json");
|
|
using var response = await client.PostAsync(url, content, cancellationToken);
|
|
response.EnsureSuccessStatusCode();
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
else
|
|
{
|
|
using var response = await client.GetAsync(url, cancellationToken);
|
|
response.EnsureSuccessStatusCode();
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|
|
|