|
|
@ -1,16 +1,15 @@ |
|
|
|
using com.alibaba.openapi.client.policy; |
|
|
|
using BBWY.Common.Http; |
|
|
|
using com.alibaba.openapi.client.entity; |
|
|
|
using com.alibaba.openapi.client.policy; |
|
|
|
using com.alibaba.openapi.client.serialize; |
|
|
|
using com.alibaba.openapi.client.util; |
|
|
|
using Newtonsoft.Json.Linq; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.IO; |
|
|
|
using System.Net; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Text; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using System.Runtime.Serialization.Json; |
|
|
|
using System.IO; |
|
|
|
using com.alibaba.openapi.client.serialize; |
|
|
|
using com.alibaba.openapi.client.entity; |
|
|
|
using System.Collections; |
|
|
|
using System.Web; |
|
|
|
|
|
|
|
namespace com.alibaba.openapi.client.http |
|
|
@ -18,12 +17,43 @@ namespace com.alibaba.openapi.client.http |
|
|
|
public class HttpClient |
|
|
|
{ |
|
|
|
private ClientPolicy clientPolicy; |
|
|
|
private RestApiService restApiService; |
|
|
|
private IDictionary<string, string> requestHeader; |
|
|
|
|
|
|
|
public HttpClient(ClientPolicy clientPolicy) |
|
|
|
{ |
|
|
|
this.clientPolicy = clientPolicy; |
|
|
|
} |
|
|
|
|
|
|
|
public HttpClient(ClientPolicy clientPolicy, RestApiService restApiService) |
|
|
|
{ |
|
|
|
this.clientPolicy = clientPolicy; |
|
|
|
this.restApiService = restApiService; |
|
|
|
this.requestHeader = new Dictionary<string, string>() |
|
|
|
{ |
|
|
|
{ "User-Agent","Ocean/NET-SDKClient"} |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
public JObject NewRequest(Request request, RequestPolicy requestPolicy) |
|
|
|
{ |
|
|
|
StringBuilder path = createProtocolRequestPath(requestPolicy, request); |
|
|
|
Dictionary<string, object> parameters = createParameterDictionary(requestPolicy, request); |
|
|
|
signature(path.ToString(), parameters, requestPolicy, clientPolicy); |
|
|
|
string paramString = createParameterStr(parameters); |
|
|
|
Uri uri = new Uri(buildRequestUri(requestPolicy, request)); |
|
|
|
|
|
|
|
var result = restApiService.SendRequest($"{uri.Scheme}://{uri.Host}", |
|
|
|
uri.LocalPath, |
|
|
|
paramString, |
|
|
|
requestHeader, |
|
|
|
requestPolicy.HttpMethod.Equals("GET") ? HttpMethod.Get : HttpMethod.Post, |
|
|
|
RestApiService.ContentType_Form); |
|
|
|
if (result.StatusCode != HttpStatusCode.OK) |
|
|
|
throw new Exception(result.Content.ToString()); |
|
|
|
return JObject.Parse(result.Content); |
|
|
|
} |
|
|
|
|
|
|
|
public T request<T>(Request request, RequestPolicy requestPolicy) |
|
|
|
{ |
|
|
|
StringBuilder path = createProtocolRequestPath(requestPolicy, request); |
|
|
@ -75,7 +105,7 @@ namespace com.alibaba.openapi.client.http |
|
|
|
HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse; |
|
|
|
Stream responseStream = response.GetResponseStream(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DeSerializer deSerializer = SerializerProvider.getInstance().getDeSerializer(requestPolicy.ResponseProtocol); |
|
|
|
ResponseWrapper rw = deSerializer.deSerialize(responseStream, typeof(T), Encoding.UTF8.EncodingName); |
|
|
|
return (T)rw.Result; |
|
|
@ -85,7 +115,7 @@ namespace com.alibaba.openapi.client.http |
|
|
|
HttpWebResponse response = webException.Response as HttpWebResponse; |
|
|
|
Stream responseStream = response.GetResponseStream(); |
|
|
|
DeSerializer deSerializer = SerializerProvider.getInstance().getDeSerializer(requestPolicy.ResponseProtocol); |
|
|
|
Exception rw = deSerializer.buildException(responseStream,500, Encoding.UTF8.EncodingName); |
|
|
|
Exception rw = deSerializer.buildException(responseStream, 500, Encoding.UTF8.EncodingName); |
|
|
|
throw rw; |
|
|
|
} |
|
|
|
} |
|
|
@ -147,7 +177,7 @@ namespace com.alibaba.openapi.client.http |
|
|
|
{ |
|
|
|
String tempValue = kvp.Value.ToString(); |
|
|
|
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(tempValue); |
|
|
|
encodedValue = HttpUtility.UrlEncode(byteArray, 0, byteArray.Length); |
|
|
|
encodedValue = HttpUtility.UrlEncode(byteArray, 0, byteArray.Length); |
|
|
|
} |
|
|
|
paramBuilder.Append(kvp.Key).Append("=").Append(encodedValue); |
|
|
|
paramBuilder.Append("&"); |
|
|
|