using Furion;
using Furion.FriendlyException;
using Furion.Templates;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.IdentityModel.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection.PortableExecutable;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
namespace BPA.KitChen.GroupMealOrder.Core.Common
{
public static class HttpHelper
{
private static readonly HttpClient client = new HttpClient();
///
/// Get 请求
///
///
///
///
///
///
///
public static string HttpGet(string url, string postDataStr, Dictionary headers, string contentType = "")
{
var result = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + postDataStr);
request.ServicePoint.Expect100Continue = false; //加了这一行代码 就OK了
request.Method = "GET";
request.ContentType = contentType;
foreach (KeyValuePair kv in headers)
{
request.Headers.Add(kv.Key, kv.Value);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
result = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
catch (Exception ex)
{
throw Oops.Oh( ex.Message);
}
return result;
}
public static string PostData(string url, string postData, Encoding encoding, string ContentType, Dictionary headers)
{
try
{
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
myReq.Method = "POST";
myReq.Timeout = 600 * 60;
byte[] byteArray = encoding.GetBytes(postData);
myReq.ContentType = ContentType;
//对键值对进行遍历
foreach (KeyValuePair kv in headers)
{
myReq.Headers.Add(kv.Key, kv.Value);
}
Stream dataStream = myReq.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
dataStream = HttpWResp.GetResponseStream();
string result = "";
using (StreamReader myStreamReader = new StreamReader(dataStream, encoding))
{
result = myStreamReader.ReadToEnd();
myStreamReader.Close();
}
dataStream.Close();
HttpWResp.Close();
return result;
}
catch (System.Exception ex)
{
throw ex;
}
}
}
}