You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.6 KiB
C#
72 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Web;
|
|
|
|
namespace DispatchWeb.Helper
|
|
{
|
|
public static class WebRequestHelper
|
|
{
|
|
public static string DoGet(string url, string userData)
|
|
{
|
|
string responseString = "";//post返回的结果
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
req.Method = "GET";
|
|
req.ContentLength = 0;
|
|
if (!string.IsNullOrWhiteSpace(userData))
|
|
{
|
|
req.Headers.Add("UserData", userData);
|
|
}
|
|
|
|
var response = req.GetResponse();
|
|
Stream streamResponse = response.GetResponseStream();
|
|
StreamReader streamRead = new StreamReader(streamResponse);
|
|
responseString = streamRead.ReadToEnd();
|
|
response.Close();
|
|
streamRead.Close();
|
|
|
|
return responseString;
|
|
}
|
|
|
|
public static string DoPost(string url, string json, string userData)
|
|
{
|
|
string responseString = "";//post返回的结果
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
req.Method = "POST";
|
|
if (!string.IsNullOrWhiteSpace(userData))
|
|
{
|
|
req.Headers.Add("UserData", userData);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(json))
|
|
{
|
|
byte[] postBytes = Encoding.UTF8.GetBytes(json);
|
|
req.ContentType = "application/json; charset=utf-8";
|
|
req.ContentLength = Encoding.UTF8.GetByteCount(json);
|
|
Stream stream = req.GetRequestStream();
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
req.Timeout = 10000;
|
|
stream.Close();
|
|
}
|
|
else
|
|
{
|
|
req.ContentLength = 0;
|
|
}
|
|
var response = req.GetResponse();
|
|
Stream streamResponse = response.GetResponseStream();
|
|
StreamReader streamRead = new StreamReader(streamResponse);
|
|
responseString = streamRead.ReadToEnd();
|
|
response.Close();
|
|
streamRead.Close();
|
|
|
|
return responseString;
|
|
}
|
|
}
|
|
} |