其他分享
首页 > 其他分享> > 接口get方法body赋值

接口get方法body赋值

作者:互联网

try
{
///URL接口地址
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/json; charset=UTF-8";
request.AutomaticDecompression = DecompressionMethods.GZip;
string Data = "{\"CorpId\":\"300014\"}";///Json参数

var type = request.GetType();
var currentMethod = type.GetProperty("CurrentMethod", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(request);
var methodType = currentMethod.GetType();
methodType.GetField("ContentBodyNotAllowed", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentMethod, false);

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(Data);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();

myStreamReader.Close();
myResponseStream.Close();

if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}

return retString;
}
catch (Exception ex)
{
return ("ERROR:" + ex.Message + ":" + ex.InnerException.Message);
}

标签:body,myStreamReader,get,request,BindingFlags,ex,var,response,赋值
来源: https://www.cnblogs.com/sanzhu/p/14972237.html