get请求参数处理
This commit is contained in:
@@ -26,6 +26,13 @@ namespace Getf_Service_Transfer_Client_Service_HttpHandler
|
||||
|
||||
try
|
||||
{
|
||||
var method = "POST";
|
||||
if (jObject["Method"] != null)
|
||||
{
|
||||
method = jObject["Method"].Value<string>();
|
||||
}
|
||||
if ("GET".Equals(method, StringComparison.OrdinalIgnoreCase))
|
||||
url = GetGetUrl(url, jObject);
|
||||
HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
|
||||
|
||||
var head = jObject["Head"];
|
||||
@@ -38,27 +45,25 @@ namespace Getf_Service_Transfer_Client_Service_HttpHandler
|
||||
{
|
||||
httpWebRequest.ContentType = jObject["ContentType"].Value<string>();
|
||||
}
|
||||
var method = "POST";
|
||||
if (jObject["Method"] != null)
|
||||
{
|
||||
method = jObject["Method"].Value<string>();
|
||||
}
|
||||
|
||||
SetHead(httpWebRequest, method, head);
|
||||
|
||||
object r;
|
||||
if (!"GET".Equals(method, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var requestStream = httpWebRequest.GetRequestStream();
|
||||
SetRequestStream(httpWebRequest, requestStream, jObject, encoding);
|
||||
r = GetResponse(httpWebRequest, jObject, encoding);
|
||||
using (var requestStream = httpWebRequest.GetRequestStream())
|
||||
{
|
||||
SetPostRequestStream(httpWebRequest, requestStream, jObject, encoding);
|
||||
r = GetResponse(httpWebRequest, encoding);
|
||||
requestStream.Close();
|
||||
requestStream.Dispose();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
r = GetResponse(httpWebRequest, null, encoding);
|
||||
r = GetResponse(httpWebRequest, encoding);
|
||||
}
|
||||
|
||||
msg = String.Empty;
|
||||
_LogHelper.Info($"93段接口:method:{method},url:{url},参数:{jObject["Data"]},结果:{r}");
|
||||
return r;
|
||||
@@ -90,7 +95,30 @@ namespace Getf_Service_Transfer_Client_Service_HttpHandler
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private string GetGetUrl(string url, JObject jObject)
|
||||
{
|
||||
if (!url.Contains("?")) url += "?1=1";
|
||||
var jParam = jObject["Param"];
|
||||
if (jParam != null)
|
||||
{
|
||||
var Param = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(jParam.ToString());
|
||||
foreach (var item in Param)
|
||||
{
|
||||
url += $"{item.Key}={item.Value}";
|
||||
}
|
||||
}
|
||||
|
||||
var jdata = jObject["Data"];
|
||||
if (jdata != null)
|
||||
{
|
||||
var Data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(jdata.ToString());
|
||||
foreach (var item in Data)
|
||||
{
|
||||
url += $"&{item.Key}={item.Value}";
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
private static PropertyInfo InnerCollectionProperty = typeof(WebHeaderCollection).GetProperty("InnerCollection",
|
||||
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
|
||||
|
||||
@@ -121,7 +149,7 @@ namespace Getf_Service_Transfer_Client_Service_HttpHandler
|
||||
}
|
||||
}
|
||||
|
||||
private void SetRequestStream(HttpWebRequest httpWebRequest, Stream requestStream, JObject jObject, Encoding encoding)
|
||||
private void SetPostRequestStream(HttpWebRequest httpWebRequest, Stream requestStream, JObject jObject, Encoding encoding)
|
||||
{
|
||||
using (var streamWrite = new StreamWriter(requestStream, encoding))
|
||||
{
|
||||
@@ -141,7 +169,7 @@ namespace Getf_Service_Transfer_Client_Service_HttpHandler
|
||||
|
||||
private static readonly string[] TextContentTypes = new string[] { "application/json", "text/html" };
|
||||
|
||||
private object GetResponse(HttpWebRequest httpWebRequest, JObject jObject, Encoding encoding)
|
||||
private object GetResponse(HttpWebRequest httpWebRequest, Encoding encoding)
|
||||
{
|
||||
var response = httpWebRequest.GetResponse();
|
||||
using (var stream = response.GetResponseStream())
|
||||
|
||||
Reference in New Issue
Block a user