.net core integration with .net framework should be sensitive to DateTime due to its format
it may lead to cannot get related value is null or mini value
sample about whole request
// startup.cs
private void InjectClients(IServiceCollection services)
{
services.AddHttpClient(ClientsConstants.TABLE_SERVICE_CLIENT, client =>
{
client.BaseAddress = new Uri("http://localhost:8080/xxx/");
client.Timeout = TimeSpan.FromMilliseconds(10 * 1000));
});
}
// util.cs
public static T Post<T>(IHttpClientFactory factory, string ClientName, object body, string servicePath)
{
var jsonBody = JsonUtil.SerializeObject(body, DateFormatHandling.MicrosoftDateFormat);
var client = factory.CreateClient(ClientName);
StringContent content = new StringContent(jsonBody);
content.Headers.ContentType = new MediaTypeHeaderValue("aplication/json");
var response = client.PostAsync(servicePath, content).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode)
{
throw new Exception("Get data from TableService failed. URI: "
+ response.RequestMessage.RequestUri + ", Error Message: " + responseContent);
}
return JsonUtil.DeserializeObject<T>(responseContent);
}
// usage: notice _clientFactory is get from IOC container
HttpUtil.Post<ReceiveTableInventory>(_clientFactory, CLIENT_NAME, a, SERVICE_PATH);
rest client: is same as http client
content-type is "application/json`
request.AddJsonBody(jsonBody);
public static IRestResponse Post(string requestPath, int timeout, object jsonBody, IDictionary<string, string> headerParams, string clientPath)
{
var request = new RestRequest(requestPath, Method.POST) { Timeout = timeout };
// request.AddHeader("Content-Type", "application/json");
// request.RequestFormat = DataFormat.Json;
if (headerParams != null && headerParams.Count > 0)
{
foreach (string key in headerParams.Keys)
{
request.AddHeader(key, headerParams[key]);
}
}
if (jsonBody != null)
{
request.AddJsonBody(jsonBody);
}
var client = new RestClient(clientPath);
return client.Execute(request);
}
content-type is "application/x-www-form-urlencoded`
public JWTTokenModel getAccessToken()
{
var request = new RestRequest(Path, Method.POST) { Timeout = 10000 };
// make no sense
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
StringBuilder sb = new StringBuilder();
sb.Append("grant_type=").Append(_grantType)
.Append("&username=").Append(_userName)
.Append("&password=").Append(_password);
var client = new RestClient(_baseUrl);
client.Authenticator = new HttpBasicAuthenticator(_oAuthClientKey, _oAuthClientSecret);
// special
request.AddParameter("application/x-www-form-urlencoded", sb.ToString(), ParameterType.RequestBody);
client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
var response = client.Execute(request);
if (response.IsSuccessful)
{
return JsonConvert.DeserializeObject<JWTTokenModel>(response.Content);
}
else
{
if (response.ErrorException != null)
{
_logger.LogWarning("Get token fialed from identity server, cause by: " + response.ErrorException.StackTrace);
}
else
{
// handle IS Server client donot exist case, which is specify
// due to IS Server will return error message in content rather than in ErrorException
_logger.LogWarning("Get token fialed from identity server, cause by: " + response.Content);
}
}
return null;
}
core: this is important, so we can use fiddler to trace this http detail
Misused header name.
application/json
http client[such as named client]
notice: it will create client for each request,so can add specify header in startup, such as Authorization.
This is outgoing direction request.
about uri:
/
of BaseAddress, should be provided/
of relative path of service, should be not provided404
about header
can set common headers in startup.cs, such as timeout
about body
.net core integration with .net framework should be sensitive to
DateTime
due to its formatit may lead to cannot get related value is null or mini value
sample about whole request
rest client: is same as http client