postman的复制代码默认就是RestSharp。方便也好用,但是使用get请求并且带Body的时候要注意,返回的数据竟然会有丢失
解决办法:
string RequestByGet(string index, string action, object paramter)
{
var api = $"{ElasticsearchUrl}/{index}/{action}";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/json; charset=UTF-8";
request.AutomaticDecompression = DecompressionMethods.GZip;
string body = InitBody("order", paramter);
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(body);
}
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;
}
标签:body,string,Get,request,BindingFlags,var,RestSharp,response
From: https://www.cnblogs.com/BOSET/p/17275877.html