C# 中GET请求,Body中携带参数

今天,对接设备的一个接口。要求在GET请求的报文中传参数。一开始我一脸懵逼,GET里还能传参数。经过一顿搜索,还真能传。

今天,对接设备的一个接口。要求在GET请求的报文中传参数。一开始我一脸懵逼,GET里还能传参数。经过一顿搜索,还真能传。代码如下:


JsonSerializerSettings JsonSettings = new JsonSerializerSettings
{
    NullValueHandling = NullValueHandling.Ignore
};
String PostData = JsonConvert.SerializeObject(request, JsonSettings);
HttpWebRequest HttpRequest = (HttpWebRequest)WebRequest.Create(Url);
HttpRequest.Timeout = 10000;
HttpRequest.KeepAlive = false;
HttpRequest.Method = "GET";
HttpRequest.ContentType = "application/json;charset=UTF-8";

Object CurrentMethod = HttpRequest.GetType()
    .GetProperty("CurrentMethod", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(HttpRequest);
CurrentMethod.GetType()
    .GetField("ContentBodyNotAllowed", BindingFlags.NonPublic | BindingFlags.Instance)
    .SetValue(CurrentMethod, false);

using (StreamWriter PostStream = new StreamWriter(HttpRequest.GetRequestStream()))
{
    PostStream.Write(PostData);
}
using (HttpWebResponse HttpResponse = (HttpWebResponse)HttpRequest.GetResponse())
using (Stream ResponseStream = HttpResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(ResponseStream, Encoding.UTF8))
{
    RultStr = reader.ReadToEnd();
}
(152)
打赏 支付红包 支付红包 微信打赏 微信打赏
上一篇 1970-01-01 08:00:00
下一篇 2023-04-03 17:20:02

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信