接口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);
}

上一篇:C#自动创建快捷方式


下一篇:c#-反射-ui对象自动为model对象赋值-简单实验