103 lines
3.4 KiB
C#
103 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Ultron.Ngrok.Utils
|
|
{
|
|
public class HttpUtils
|
|
{
|
|
public static void HttpGet(string url)
|
|
{
|
|
WebClient wc = new WebClient();
|
|
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
|
|
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
|
|
wc.OpenReadAsync(uri);
|
|
}
|
|
public static void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
|
|
{
|
|
StreamReader read = new StreamReader(e.Result);
|
|
string str = read.ReadToEnd();
|
|
}
|
|
|
|
public static void HttpPostAsync(string url,string postData)
|
|
{
|
|
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
|
|
request.Timeout = 30000;
|
|
request.Method = "post"; ;
|
|
request.ContentType = "application/json;charset=UTF-8";
|
|
StatePara para = new StatePara
|
|
{
|
|
RequestInfo = request,
|
|
PostData = postData
|
|
};
|
|
request.BeginGetRequestStream(GetRequestStreamCallback, para);
|
|
}
|
|
|
|
private static void GetRequestStreamCallback(IAsyncResult asyncResult)
|
|
{
|
|
StatePara para = (StatePara)asyncResult.AsyncState;
|
|
try
|
|
{
|
|
para.RequestInfo.BeginGetResponse(GetResponseCallback, para);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string retString = ex.Message;
|
|
}
|
|
}
|
|
private static void GetResponseCallback(IAsyncResult asyncResult)
|
|
{
|
|
string retString = "";
|
|
StatePara para = (StatePara)asyncResult.AsyncState;
|
|
IAsyncResult IAResult = para.RequestInfo as IAsyncResult;
|
|
try
|
|
{
|
|
HttpWebRequest request = para.RequestInfo;
|
|
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
|
|
|
|
|
|
if (response.ContentEncoding.ToLower().Contains("gzip"))
|
|
{
|
|
using (var gzip = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
|
|
{
|
|
|
|
var reader = new StreamReader(gzip, Encoding.GetEncoding("utf-8"));
|
|
retString = reader.ReadToEnd();
|
|
reader.Close();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Stream myResponseStream = response.GetResponseStream();
|
|
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
|
retString = myStreamReader.ReadToEnd();
|
|
myStreamReader.Close();
|
|
myResponseStream.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
retString = ex.Message;
|
|
}
|
|
}
|
|
|
|
class StatePara
|
|
{
|
|
public HttpWebRequest RequestInfo { get; set; }
|
|
|
|
public string PostData { get; set; }
|
|
|
|
public string ServerType { get; set; }
|
|
|
|
}
|
|
}
|
|
}
|