103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Pilot_KD_Parino.Common
|
||
{
|
||
public class HttpClient
|
||
{
|
||
/// <summary>
|
||
/// Seivice URL
|
||
/// </summary>
|
||
public string Url { get; set; }
|
||
/// <summary>
|
||
/// 内容
|
||
/// </summary>
|
||
public string Content { get; set; }
|
||
|
||
|
||
/// <summary>
|
||
/// Token
|
||
/// </summary>
|
||
public string Token { get; set; }
|
||
|
||
/// <summary>
|
||
/// Cookie,保证登录后,所有访问持有一个Cookie;
|
||
/// </summary>
|
||
static CookieContainer Cookie = new CookieContainer();
|
||
|
||
/// <summary>
|
||
/// HTTP访问
|
||
/// </summary>
|
||
public string Post()
|
||
{
|
||
HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest;
|
||
httpRequest.Method = "POST";
|
||
httpRequest.ContentType = "application/json";
|
||
httpRequest.CookieContainer = Cookie;
|
||
httpRequest.Timeout = 1000 * 60 * 10;//10min
|
||
|
||
//httpRequest.Headers.Add("JWT", string.Format("Bearer {0}", Token==null?"": Token.Trim().Replace("\"", "")));
|
||
httpRequest.Headers.Add("jwt", string.Format("Bearer {0}", Token == null ? "" : Token.Trim().Replace("\"", "")));
|
||
|
||
using (Stream reqStream = httpRequest.GetRequestStream())
|
||
{
|
||
JObject jObj = new JObject();
|
||
//jObj.Add("format", 1);
|
||
//jObj.Add("useragent", "ApiClient");
|
||
//jObj.Add("rid", Guid.NewGuid().ToString().GetHashCode().ToString());
|
||
//jObj.Add("parameters", Content);
|
||
//jObj.Add("timestamp", DateTime.Now);
|
||
//jObj.Add("v", "1.0");
|
||
string sContent = jObj.ToString();
|
||
var bytes = UnicodeEncoding.UTF8.GetBytes(Content);
|
||
reqStream.Write(bytes, 0, bytes.Length);
|
||
reqStream.Flush();
|
||
}
|
||
using (var repStream = httpRequest.GetResponse().GetResponseStream())
|
||
{
|
||
using (var reader = new StreamReader(repStream))
|
||
{
|
||
return ValidateResult(reader.ReadToEnd());
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
public string Get()
|
||
{
|
||
string result = string.Empty;
|
||
string error = string.Empty;
|
||
|
||
HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest;
|
||
httpRequest.Timeout = 20000;
|
||
httpRequest.Method = "GET";
|
||
httpRequest.KeepAlive = true;
|
||
httpRequest.Headers.Add("Authorization", "token");
|
||
|
||
HttpWebResponse reponse = (HttpWebResponse)httpRequest.GetResponse();
|
||
string PostContent = new StreamReader(reponse.GetResponseStream()).ReadToEnd();
|
||
return PostContent;
|
||
|
||
}
|
||
|
||
|
||
|
||
private static string ValidateResult(string responseText)
|
||
{
|
||
if (responseText.StartsWith("response_error:"))
|
||
{
|
||
return responseText.TrimStart("response_error:".ToCharArray());
|
||
}
|
||
return responseText;
|
||
}
|
||
}
|
||
}
|