【http请求的“带”参代码】
public static string PostRequest(string Url, string Param)
{
HttpWebRequest request;
HttpWebResponse response; string strURL = Url; string StrDate = "";
string strValue = "";
try
{
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/json;";
request.Timeout = 20000;
byte[] Data = Encoding.UTF8.GetBytes(Param);
request.GetRequestStream().Write(Data, 0, Data.Length);
response = (HttpWebResponse)request.GetResponse();
request.GetRequestStream().Close();
}
catch (WebException ex)
{
strValue = ex.Message;
response = (HttpWebResponse)ex.Response;
}
try
{
Stream Data_S = response.GetResponseStream();
StreamReader Reader = new StreamReader(Data_S, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
Data_S.Close();
}
catch (Exception ex)
{
strValue = ex.Message;
}
return strValue; }
【http请求的“无”参代码】
public static string PostRequest(string Url)
{
HttpWebRequest request;
HttpWebResponse response; string strURL = Url;
string StrDate = "";
string strValue = "";
try
{
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
strValue = ex.Message;
response = (HttpWebResponse)ex.Response;
}
try
{
Stream Data_S = response.GetResponseStream(); StreamReader Reader = new StreamReader(Data_S, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
Data_S.Close(); }
catch (Exception ex)
{
strValue = ex.Message;
}
return strValue;
}
接下来就是【RestSharp.dll】 请求示例
public static string PostRequest(string Url, string Param)
{
string strValue = string.Empty;
try
{
var client = new RestClient(Url);
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
var body = Param;
request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(body), ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var responseContent = response.Content;
strValue = responseContent;
}
catch (Exception ex)
{
strValue = ex.Message;
}
return strValue;
}
【RestSharp.dll】下载地址
[点我直接下载,如下载不了,请给我留言,我会第一时间更新下载地址,密码:Shunli]
本文作者:[博主]大顺
本文链接:https://shunnet.top/Y3aaa2
版权声明:转载注明出处,谢谢