Home > front end >  The response. The content. ReadAsStringAsync (). The Result return Json text into assets that charac
The response. The content. ReadAsStringAsync (). The Result return Json text into assets that charac

Time:11-30

Private void HttpSendRequest (string HTTPMethods, string RequestURL, string epicorLogin, string XAPIKey, string jsonStr, ref string ResponseStatusCode, ref string ResponseBody, ref string IsSuccessStatusCode, ref string ErrorMessage, ref string ExceptionMsg)
{
Try
{
//Ignore SSL certificates
Var handler=new WebRequestHandler ();
Handler. ServerCertificateValidationCallback delegate of={return true; };

Using (HttpClient client=new HttpClient (handler))
{
Client. DefaultRequestHeaders. Authorization=new AuthenticationHeaderValue (" Basic ", Convert ToBase64String (ASCIIEncoding. ASCII. GetBytes (epicorLogin)));
//Add the an the Accept header for the JSON format.
Client. DefaultRequestHeaders. Accept. The Add (new MediaTypeWithQualityHeaderValue (" application/json "));
Client. DefaultRequestHeaders. Add (" X - API - the Key, "XAPIKey);
String TokenCreateURL=RequestURL;
Var request=new HttpRequestMessage ();
HttpResponseMessage response=null;

If (HTTPMethods=="POST")
{
Request. RequestUri=new Uri (TokenCreateURL);
Request. The Method=HttpMethod. Post;

//send the user credential
Request. The Content=new StringContent (jsonStr, Encoding UTF8, "application/json");

The response=client. SendAsync (request). The Result;



ResponseStatusCode=the Convert. ToString ((int) response. StatusCode);
}

If (HTTPMethods=="DELETE")
{
The response=client. DeleteAsync (TokenCreateURL). The Result;
ResponseStatusCode=the Convert. ToString ((int) response. StatusCode);
}

If (HTTPMethods=="GET")
{
The response=client. GetAsync (TokenCreateURL). The Result;
ResponseStatusCode=the Convert. ToString ((int) response. StatusCode);
}

//Get the response


If (response. IsSuccessStatusCode)
{
IsSuccessStatusCode="Yes";
}
The else
{
IsSuccessStatusCode="No".
}

ResponseBody=response. Content. ReadAsStringAsync (). The Result;

//Deserialize ResponseBody and catch "ErrorMessage"
If (ResponseBody. IndexOf (" \ "ErrorMessage " ")> 0)
{
The dynamic dyn=Newtonsoft. Json. JsonConvert. DeserializeObject (ResponseBody);

The foreach (var obj in dyn)
{
If (obj. Name=="ErrorMessage")
{
ErrorMessage=the Convert. ToString (obj. Value);
break;
}
}
}
}
}
The catch (AggregateException ex)
{
ExceptionMsg=ex. ToString ();
}
}
  •  Tags:  
  • ASP
  • Related