368 lines
12 KiB
C#
368 lines
12 KiB
C#
using MesWork;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace PLMTEST
|
||
{
|
||
public class HttpCli
|
||
{
|
||
|
||
|
||
/// <summary>
|
||
/// 指定Url地址使用Get 方式获取全部字符串
|
||
/// </summary>
|
||
/// <param name="url">请求链接地址</param>
|
||
/// <returns></returns>
|
||
public static string Get(string url)
|
||
{
|
||
string result = "";
|
||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||
Stream stream = resp.GetResponseStream();
|
||
try
|
||
{
|
||
//获取内容
|
||
using (StreamReader reader = new StreamReader(stream))
|
||
{
|
||
result = reader.ReadToEnd();
|
||
}
|
||
}
|
||
finally
|
||
{
|
||
stream.Close();
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static void GetFile(string url,string path)
|
||
{
|
||
WebRequest request = WebRequest.Create(url);
|
||
WebResponse response = request.GetResponse();
|
||
if (response.ContentType.ToLower().Length > 0)
|
||
{
|
||
using (Stream reader = response.GetResponseStream())
|
||
{
|
||
using (FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
|
||
{
|
||
byte[] buffer = new byte[1024];
|
||
int c = 0;
|
||
while ((c = reader.Read(buffer, 0, buffer.Length)) > 0)
|
||
{
|
||
writer.Write(buffer, 0, c);
|
||
}
|
||
}
|
||
}
|
||
//HttpContext
|
||
}
|
||
}
|
||
|
||
public static byte[] GetFile_Bytes(string url)
|
||
{
|
||
byte[] bytesAll = null;
|
||
WebRequest request = WebRequest.Create(url);
|
||
WebResponse response = request.GetResponse();
|
||
if (response.ContentType.ToLower().Length > 0)
|
||
{
|
||
using (Stream reader = response.GetResponseStream())
|
||
{
|
||
MemoryStream ms = new MemoryStream();
|
||
byte[] buffer = new byte[1024];
|
||
while (true)
|
||
{
|
||
int sz = reader.Read(buffer, 0, 1024);
|
||
if (sz == 0) break;
|
||
ms.Write(buffer, 0, sz);
|
||
}
|
||
bytesAll = ms.ToArray();
|
||
}
|
||
}
|
||
|
||
//如是图片
|
||
//System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
|
||
|
||
return bytesAll;
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 发送Get请求
|
||
/// </summary>
|
||
/// <param name="url">地址</param>
|
||
/// <param name="dic">请求参数定义</param>
|
||
/// <returns></returns>
|
||
public static string Get(string url, Dictionary<string, string> dic)
|
||
{
|
||
string result = "";
|
||
StringBuilder builder = new StringBuilder();
|
||
builder.Append(url);
|
||
if (dic.Count > 0)
|
||
{
|
||
builder.Append("?");
|
||
int i = 0;
|
||
foreach (var item in dic)
|
||
{
|
||
if (i > 0)
|
||
builder.Append("&");
|
||
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
||
i++;
|
||
}
|
||
}
|
||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
|
||
//添加参数
|
||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||
Stream stream = resp.GetResponseStream();
|
||
try
|
||
{
|
||
//获取内容
|
||
using (StreamReader reader = new StreamReader(stream))
|
||
{
|
||
result = reader.ReadToEnd();
|
||
}
|
||
}
|
||
finally
|
||
{
|
||
stream.Close();
|
||
}
|
||
return result;
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 指定Post地址使用Get 方式获取全部字符串
|
||
/// </summary>
|
||
/// <param name="url">请求后台地址</param>
|
||
/// <returns></returns>
|
||
public static string Post(string url)
|
||
{
|
||
string result = "";
|
||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||
req.Method = "POST";
|
||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||
Stream stream = resp.GetResponseStream();
|
||
//获取内容
|
||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||
{
|
||
result = reader.ReadToEnd();
|
||
}
|
||
return result;
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 指定Post地址使用Get 方式获取全部字符串
|
||
/// </summary>
|
||
/// <param name="url">请求后台地址</param>
|
||
/// <returns></returns>
|
||
public static string Post(string url, Dictionary<string, string> dic)
|
||
{
|
||
string result = "";
|
||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||
req.Method = "POST";
|
||
req.ContentType = "application/x-www-form-urlencoded";
|
||
#region 添加Post 参数
|
||
StringBuilder builder = new StringBuilder();
|
||
int i = 0;
|
||
foreach (var item in dic)
|
||
{
|
||
if (i > 0)
|
||
builder.Append("&");
|
||
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
||
i++;
|
||
}
|
||
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
|
||
req.ContentLength = data.Length;
|
||
using (Stream reqStream = req.GetRequestStream())
|
||
{
|
||
reqStream.Write(data, 0, data.Length);
|
||
reqStream.Close();
|
||
}
|
||
#endregion
|
||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||
Stream stream = resp.GetResponseStream();
|
||
//获取响应内容
|
||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||
{
|
||
result = reader.ReadToEnd();
|
||
}
|
||
return result;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 指定Post地址使用Get 方式获取全部字符串
|
||
/// </summary>
|
||
/// <param name="url">请求后台地址</param>
|
||
/// <param name="content">Post提交数据内容(utf-8编码的)</param>
|
||
/// <returns></returns>
|
||
public static string Post_Authorization(string url, string content,out string error )
|
||
{
|
||
string result = "";
|
||
error = "";
|
||
try
|
||
{
|
||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||
req.Timeout = 30000;
|
||
req.Method = "POST";
|
||
req.ContentType = "application/json";
|
||
|
||
// 添加自定义 Header
|
||
string authorization = ConfigurationManager.AppSettings["AuthorizationSet"];
|
||
if (!string.IsNullOrWhiteSpace(authorization))
|
||
{
|
||
req.Headers.Add("Authorization", authorization);
|
||
}
|
||
req.Headers.Add("uuid", Guid.NewGuid().ToString().ToUpper());
|
||
|
||
#region 添加Post 参数
|
||
byte[] data = Encoding.UTF8.GetBytes(content);
|
||
req.ContentLength = data.Length;
|
||
using (Stream reqStream = req.GetRequestStream())
|
||
{
|
||
reqStream.Write(data, 0, data.Length);
|
||
reqStream.Close();
|
||
}
|
||
#endregion
|
||
|
||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||
Stream stream = resp.GetResponseStream();
|
||
//获取响应内容
|
||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||
{
|
||
result = reader.ReadToEnd();
|
||
}
|
||
}
|
||
catch (Exception err)
|
||
{
|
||
error = $"WebApi Post请求调用错误:{err.Message}";
|
||
}
|
||
|
||
return result;
|
||
}
|
||
public static string Post(string url, string content, out string error)
|
||
{
|
||
string result = "";
|
||
error = "";
|
||
try
|
||
{
|
||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||
req.Timeout = 30000;
|
||
req.Method = "POST";
|
||
req.ContentType = "application/json";
|
||
|
||
#region 添加Post 参数
|
||
byte[] data = Encoding.UTF8.GetBytes(content);
|
||
req.ContentLength = data.Length;
|
||
using (Stream reqStream = req.GetRequestStream())
|
||
{
|
||
reqStream.Write(data, 0, data.Length);
|
||
reqStream.Close();
|
||
}
|
||
#endregion
|
||
|
||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||
Stream stream = resp.GetResponseStream();
|
||
//获取响应内容
|
||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||
{
|
||
result = reader.ReadToEnd();
|
||
}
|
||
}
|
||
catch (Exception err)
|
||
{
|
||
error = $"WebApi Post请求调用错误:{err.Message}";
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// Http下载文件
|
||
/// </summary>
|
||
/// <param name="uri">下载地址</param>
|
||
/// <param name="filefullpath">存放完整路径(含文件名)</param>
|
||
/// <param name="size">每次多的大小</param>
|
||
/// <returns>下载操作是否成功</returns>
|
||
|
||
public static bool DownLoadFiles(string uri, string filefullpath, int size = 1000000)
|
||
{
|
||
try
|
||
{
|
||
if (File.Exists(filefullpath))
|
||
{
|
||
try
|
||
{
|
||
File.Delete(filefullpath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
string fileDirectory = System.IO.Path.GetDirectoryName(filefullpath);
|
||
|
||
if (!Directory.Exists(fileDirectory))
|
||
{
|
||
Directory.CreateDirectory(fileDirectory);
|
||
}
|
||
|
||
FileStream fs = new FileStream(filefullpath, FileMode.Create);
|
||
|
||
byte[] buffer = new byte[size];
|
||
|
||
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
|
||
|
||
request.Timeout = 100000;
|
||
|
||
request.AddRange((int)fs.Length);
|
||
|
||
Stream ns = request.GetResponse().GetResponseStream();
|
||
|
||
long contentLength = request.GetResponse().ContentLength;
|
||
|
||
int length = ns.Read(buffer, 0, buffer.Length);
|
||
|
||
while (length > 0)
|
||
{
|
||
fs.Write(buffer,0 , length);
|
||
buffer = new byte[size];
|
||
length = ns.Read(buffer, 0, buffer.Length);
|
||
}
|
||
|
||
fs.Close();
|
||
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
}
|