286 lines
11 KiB
C#
286 lines
11 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Specialized;
|
||
using System.IO;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Web;
|
||
|
||
namespace BLL
|
||
{
|
||
public static class APIGetHttpService
|
||
{
|
||
/// <summary>
|
||
/// 不做catch处理,需要在外部做
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="method">默认GET,空则补充为GET</param>
|
||
/// <param name="contenttype">默认json,空则补充为json</param>
|
||
/// <param name="header">请求头部</param>
|
||
/// <param name="data">请求body内容</param>
|
||
/// <returns></returns>
|
||
public static string Http(string url, string method = "GET", string contenttype = "application/json;charset=utf-8", Hashtable header = null, string data = null)
|
||
{
|
||
if (url.IndexOf("https") >= 0)
|
||
{
|
||
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
||
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
|
||
|
||
}
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||
request.Method = string.IsNullOrEmpty(method) ? "GET" : method;
|
||
request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/json;charset=utf-8" : contenttype;
|
||
if (url.IndexOf("https") >= 0)
|
||
{
|
||
request.ProtocolVersion = HttpVersion.Version10;
|
||
}
|
||
if (header != null)
|
||
{
|
||
foreach (var i in header.Keys)
|
||
{
|
||
request.Headers.Add(i.ToString(), header[i].ToString());
|
||
}
|
||
}
|
||
else
|
||
{
|
||
request.Headers.Add("token", "AF17168B-87BD-4GLY-1111-F0A0A1158F9B");
|
||
}
|
||
if (!string.IsNullOrEmpty(data))
|
||
{
|
||
Stream RequestStream = request.GetRequestStream();
|
||
byte[] bytes = Encoding.UTF8.GetBytes(data);
|
||
RequestStream.Write(bytes, 0, bytes.Length);
|
||
RequestStream.Close();
|
||
}
|
||
|
||
HttpWebResponse response = null;
|
||
Stream ResponseStream = null;
|
||
StreamReader StreamReader = null;
|
||
try
|
||
{
|
||
response = (HttpWebResponse)request.GetResponse();
|
||
ResponseStream = response.GetResponseStream();
|
||
StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
|
||
|
||
string re = StreamReader.ReadToEnd();
|
||
StreamReader.Close();
|
||
ResponseStream.Close();
|
||
return re;
|
||
}
|
||
catch (WebException ex)
|
||
{
|
||
response = (HttpWebResponse)ex.Response;
|
||
ResponseStream = response.GetResponseStream();
|
||
StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
|
||
|
||
string re = StreamReader.ReadToEnd();
|
||
return re;
|
||
}
|
||
finally
|
||
{
|
||
if (StreamReader != null)
|
||
{
|
||
StreamReader.Close();
|
||
}
|
||
if (ResponseStream != null)
|
||
{
|
||
ResponseStream.Close();
|
||
}
|
||
if (response != null)
|
||
{
|
||
response.Close();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 不做catch处理,需要在外部做
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="method">默认GET,空则补充为GET</param>
|
||
/// <param name="contenttype">默认json,空则补充为json</param>
|
||
/// <param name="header">请求头部</param>
|
||
/// <param name="data">请求body内容</param>
|
||
/// <returns></returns>
|
||
public static string OutsideHttp(string url, string method = "GET", string contenttype = "application/json;charset=utf-8", Hashtable header = null, string data = null)
|
||
{
|
||
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||
request.Method = string.IsNullOrEmpty(method) ? "GET" : method;
|
||
request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/json;charset=utf-8" : contenttype;
|
||
if (header != null)
|
||
{
|
||
foreach (var i in header.Keys)
|
||
{
|
||
request.Headers.Add(i.ToString(), header[i].ToString());
|
||
}
|
||
}
|
||
if (!string.IsNullOrEmpty(data))
|
||
{
|
||
Stream RequestStream = request.GetRequestStream();
|
||
byte[] bytes = Encoding.UTF8.GetBytes(data);
|
||
RequestStream.Write(bytes, 0, bytes.Length);
|
||
RequestStream.Close();
|
||
}
|
||
|
||
HttpWebResponse response = null;
|
||
Stream ResponseStream = null;
|
||
StreamReader StreamReader = null;
|
||
try
|
||
{
|
||
response = (HttpWebResponse)request.GetResponse();
|
||
ResponseStream = response.GetResponseStream();
|
||
StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
|
||
|
||
string re = StreamReader.ReadToEnd();
|
||
return re;
|
||
}
|
||
catch (WebException ex)
|
||
{
|
||
response = (HttpWebResponse)ex.Response;
|
||
ResponseStream = response.GetResponseStream();
|
||
StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
|
||
|
||
string re = StreamReader.ReadToEnd();
|
||
return re;
|
||
}
|
||
finally
|
||
{
|
||
if (StreamReader != null)
|
||
{
|
||
StreamReader.Close();
|
||
}
|
||
if (ResponseStream != null)
|
||
{
|
||
ResponseStream.Close();
|
||
}
|
||
if (response != null)
|
||
{
|
||
response.Close();
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
private static readonly Encoding DEFAULTENCODE = Encoding.UTF8;
|
||
|
||
/// <summary>
|
||
/// HttpUploadFile
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="file"></param>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
public static string HttpUploadFile(string url, string file, NameValueCollection data)
|
||
{
|
||
return HttpUploadFile(url, file, data, DEFAULTENCODE);
|
||
}
|
||
|
||
/// <summary>
|
||
/// HttpUploadFile
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="file"></param>
|
||
/// <param name="data"></param>
|
||
/// <param name="encoding"></param>
|
||
/// <returns></returns>
|
||
public static string HttpUploadFile(string url, string file, NameValueCollection data, Encoding encoding)
|
||
{
|
||
return HttpUploadFile(url, new string[] { file }, data, encoding);
|
||
}
|
||
|
||
/// <summary>
|
||
/// HttpUploadFile
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="files"></param>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
public static string HttpUploadFile(string url, string[] files, NameValueCollection data)
|
||
{
|
||
return HttpUploadFile(url, files, data, DEFAULTENCODE);
|
||
}
|
||
|
||
/// <summary>
|
||
/// HttpUploadFile
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="files"></param>
|
||
/// <param name="data"></param>
|
||
/// <param name="encoding"></param>
|
||
/// <returns></returns>
|
||
public static string HttpUploadFile(string url, string[] files, NameValueCollection data, Encoding encoding)
|
||
{
|
||
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
|
||
byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
|
||
byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
|
||
|
||
//1.HttpWebRequest
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||
request.ContentType = "multipart/form-data; boundary=" + boundary;
|
||
request.Method = "POST";
|
||
request.KeepAlive = true;
|
||
request.Credentials = CredentialCache.DefaultCredentials;
|
||
request.Headers.Add("token", "AF17168B-87BD-4GLY-1111-F0A0A1158F9B");
|
||
|
||
using (Stream stream = request.GetRequestStream())
|
||
{
|
||
//1.1 key/value
|
||
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
|
||
if (data != null)
|
||
{
|
||
foreach (string key in data.Keys)
|
||
{
|
||
stream.Write(boundarybytes, 0, boundarybytes.Length);
|
||
string formitem = string.Format(formdataTemplate, key, data[key]);
|
||
byte[] formitembytes = encoding.GetBytes(formitem);
|
||
stream.Write(formitembytes, 0, formitembytes.Length);
|
||
}
|
||
}
|
||
|
||
//1.2 file
|
||
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
|
||
byte[] buffer = new byte[4096];
|
||
int bytesRead = 0;
|
||
for (int i = 0; i < files.Length; i++)
|
||
{
|
||
stream.Write(boundarybytes, 0, boundarybytes.Length);
|
||
string header = "";
|
||
if (i > 0)
|
||
{
|
||
header = string.Format(headerTemplate, "img", Path.GetFileName(files[i]));
|
||
}
|
||
else
|
||
{
|
||
header = string.Format(headerTemplate, "img" + i, Path.GetFileName(files[i]));
|
||
|
||
}
|
||
byte[] headerbytes = encoding.GetBytes(header);
|
||
stream.Write(headerbytes, 0, headerbytes.Length);
|
||
using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
|
||
{
|
||
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
|
||
{
|
||
stream.Write(buffer, 0, bytesRead);
|
||
}
|
||
}
|
||
}
|
||
|
||
//1.3 form end
|
||
stream.Write(endbytes, 0, endbytes.Length);
|
||
}
|
||
//2.WebResponse
|
||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
|
||
{
|
||
return stream.ReadToEnd();
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
}
|