17using System.Threading;
18using System.Threading.Tasks;
54 _httpClient = httpClient;
63 public HttpProxy(HttpClient httpClient,
string dataPortalUrl)
65 _httpClient = httpClient;
69 private static HttpClient _httpClient;
77 return new HttpClientHandler();
86 if (_httpClient ==
null)
91 handler =
new HttpClientHandler();
95 handler =
new HttpClientHandler()
97 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
100 _httpClient =
new HttpClient(handler);
116 return new DefaultWebClient(this.
Timeout);
134 protected override async Task<byte[]>
CallDataPortalServer(
byte[] serialized,
string operation,
string routingToken,
bool isSync)
137 ? CallViaWebClient(serialized, operation, routingToken)
138 : await CallViaHttpClient(serialized, operation, routingToken);
149 private async Task<byte[]> CallViaHttpClient(
byte[] serialized,
string operation,
string routingToken)
152 HttpRequestMessage httpRequest;
153 httpRequest =
new HttpRequestMessage(
155 $
"{DataPortalUrl}?operation={CreateOperationTag(operation, ApplicationContext.VersionRoutingTag, routingToken)}");
158 httpRequest.Content =
new StringContent(System.Convert.ToBase64String(serialized));
160 httpRequest.Content =
new ByteArrayContent(serialized);
161 var httpResponse = await client.SendAsync(httpRequest);
162 await VerifyResponseSuccess(httpResponse);
164 serialized = Convert.FromBase64String(await httpResponse.Content.ReadAsStringAsync());
166 serialized = await httpResponse.Content.ReadAsByteArrayAsync();
170 private byte[] CallViaWebClient(
byte[] serialized,
string operation,
string routingToken)
173 var url = $
"{DataPortalUrl}?operation={CreateOperationTag(operation, ApplicationContext.VersionRoutingTag, routingToken)}";
176 var result = client.UploadString(url, System.Convert.ToBase64String(serialized));
177 serialized = System.Convert.FromBase64String(result);
181 var result = client.UploadData(url, serialized);
187 private static async Task VerifyResponseSuccess(HttpResponseMessage httpResponse)
189 if (!httpResponse.IsSuccessStatusCode)
191 var message =
new StringBuilder();
192 message.Append((
int)httpResponse.StatusCode);
193 message.Append(
": ");
194 message.Append(httpResponse.ReasonPhrase);
195 var content = await httpResponse.Content.ReadAsStringAsync();
196 if (!
string.IsNullOrWhiteSpace(content))
198 message.AppendLine();
199 message.Append(content);
201 throw new HttpRequestException(message.ToString());
205 private string CreateOperationTag(
string operatation,
string versionToken,
string routingToken)
207 if (!
string.IsNullOrWhiteSpace(versionToken) || !
string.IsNullOrWhiteSpace(routingToken))
208 return $
"{operatation}/{routingToken}-{versionToken}";
212 private class DefaultWebClient : WebClient
214 private int Timeout {
get;
set; }
216 public DefaultWebClient(
int timeout)
221 protected override WebRequest GetWebRequest(Uri address)
223 var req = base.GetWebRequest(address) as HttpWebRequest;
Implements a data portal proxy to relay data portal calls to a remote application server.
virtual int Timeout
Gets or sets the Client timeout in milliseconds (0 uses default timeout).
string DataPortalUrl
Gets the URL address for the data portal server used by this proxy instance.
Implements a data portal proxy to relay data portal calls to a remote application server by using htt...
virtual void SetHttpRequestHeaders(HttpRequestMessage request)
Override to set headers or other properties of the HttpRequestMessage before it is sent to the server...
virtual HttpClient GetHttpClient()
Gets an HttpClient object for use in communication with the server.
HttpProxy(string dataPortalUrl)
Creates an instance of the object, initializing it to use the supplied URL.
virtual WebClient GetWebClient()
Gets an WebClient object for use in communication with the server.
HttpProxy()
Creates an instance of the object, initializing it to use the DefaultUrl values.
static bool UseTextSerialization
Gets or sets a value indicating whether to use text/string serialization instead of the default binar...
virtual HttpClientHandler GetHttpClientHandler()
Gets an HttpClientHandler for use in initializing the HttpClient instance.
HttpProxy(HttpClient httpClient, string dataPortalUrl)
Creates an instance of the object, initializing it to use the supplied HttpClient object and URL.
HttpProxy(HttpClient httpClient)
Creates an instance of the object, initializing it to use the supplied HttpClient object.
override async Task< byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync)
Select client to make request based on isSync parameter and return response from server
@ TimeSpan
TimeSpan - time span