|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using KGIS.Framework.Utils;
|
|
|
|
|
|
|
|
|
|
namespace Kingo.PluginServiceInterface.Helper
|
|
|
|
|
{
|
|
|
|
|
public static class HttpHelper
|
|
|
|
|
{
|
|
|
|
|
public static T PostResponse<T>(string url, object Data)
|
|
|
|
|
where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
T ret = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
HttpWebRequest reqest = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
reqest.Method = "POST";
|
|
|
|
|
reqest.ContentType = "application/json";
|
|
|
|
|
Stream stream = reqest.GetRequestStream();
|
|
|
|
|
byte[] bs = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Data));
|
|
|
|
|
stream.Write(bs, 0, bs.Length);
|
|
|
|
|
stream.Flush();
|
|
|
|
|
stream.Close();
|
|
|
|
|
HttpWebResponse response = (HttpWebResponse)reqest.GetResponse();
|
|
|
|
|
Stream stream1 = response.GetResponseStream();
|
|
|
|
|
//获取响应内容
|
|
|
|
|
string result = string.Empty;
|
|
|
|
|
using (StreamReader reader = new StreamReader(stream1, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
result = reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
ret = JsonConvert.DeserializeObject<T>(result);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LogAPI.Debug("PostResponse异常:" + ex.Message);
|
|
|
|
|
LogAPI.Debug("PostResponse异常:" + ex.StackTrace);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// get请求 返回类型变量
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static T GetResponse<T>(string url)
|
|
|
|
|
where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
if (url.StartsWith("https"))
|
|
|
|
|
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
|
|
|
|
|
|
|
var httpClient = new HttpClient();
|
|
|
|
|
//httpClient.DefaultRequestHeaders.Accept.Add(
|
|
|
|
|
// new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
|
T result = default(T);
|
|
|
|
|
HttpResponseMessage response = httpClient.GetAsync(url).Result;
|
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
Task<string> t = response.Content.ReadAsStringAsync();
|
|
|
|
|
string str = t.Result;
|
|
|
|
|
|
|
|
|
|
result = JsonConvert.DeserializeObject<T>(str);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
public static Task<T> GetResponseAsync<T>(string url)
|
|
|
|
|
where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
var task = Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
if (url.StartsWith("https"))
|
|
|
|
|
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
|
|
|
|
|
|
|
var httpClient = new HttpClient();
|
|
|
|
|
//httpClient.DefaultRequestHeaders.Accept.Add(
|
|
|
|
|
// new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
|
T result = default(T);
|
|
|
|
|
HttpResponseMessage response = httpClient.GetAsync(url).Result;
|
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
Task<string> t = response.Content.ReadAsStringAsync();
|
|
|
|
|
string str = t.Result;
|
|
|
|
|
|
|
|
|
|
result = JsonConvert.DeserializeObject<T>(str);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Task<T> PostResponseAsync<T>(string url, object Data)
|
|
|
|
|
where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
var task = Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
HttpWebRequest reqest = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
reqest.Method = "POST";
|
|
|
|
|
reqest.ContentType = "application/json";
|
|
|
|
|
Stream stream = reqest.GetRequestStream();
|
|
|
|
|
byte[] bs = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Data));
|
|
|
|
|
stream.Write(bs, 0, bs.Length);
|
|
|
|
|
stream.Flush();
|
|
|
|
|
stream.Close();
|
|
|
|
|
HttpWebResponse response = (HttpWebResponse)reqest.GetResponse();
|
|
|
|
|
Stream stream1 = response.GetResponseStream();
|
|
|
|
|
//获取响应内容
|
|
|
|
|
string result = string.Empty;
|
|
|
|
|
using (StreamReader reader = new StreamReader(stream1, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
result = reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
T ret = JsonConvert.DeserializeObject<T>(result);
|
|
|
|
|
return ret;
|
|
|
|
|
});
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public class ResultInfo<T>
|
|
|
|
|
{
|
|
|
|
|
public T Data { get; set; }
|
|
|
|
|
|
|
|
|
|
public int Total { get; set; }
|
|
|
|
|
|
|
|
|
|
public int Code { get; set; }
|
|
|
|
|
|
|
|
|
|
public int Status { get; set; }
|
|
|
|
|
|
|
|
|
|
public string Error { get; set; }
|
|
|
|
|
public string Message { get; set; }
|
|
|
|
|
public int SuccessCount { get; set; }
|
|
|
|
|
public int FailCount { get; set; }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|