Advertisement
suhendrayputra

C# Api Client

Sep 22nd, 2022
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | Source Code | 0 0
  1. public static bool CertValidation(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslpolicyErrors)
  2. {
  3.     bool result = true;
  4.     // TODO: implement
  5.     return result;
  6. }
  7.  
  8. public static string HttpPostJSonString(string url, string postData)//user_key untuk ws vclaim 2
  9. {
  10.     string responseFromServer = "";
  11.     try
  12.     {
  13.         System.Net.ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertValidation);
  14.         HttpWebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
  15.         // Set the Method property of the request to POST.
  16.         request.Method = "POST";
  17.         //req.Timeout = 10000;// 10 seconds // belum ada issue jangan dipasang dulu
  18.         //req.Headers.Add("Accept", "application/json");
  19.         request.Headers.Add("X-cons-id", xConsId);
  20.         request.Headers.Add("X-timestamp", xTimeStamp);
  21.         request.Headers.Add("X-signature", xSignature);
  22.         // Create POST data and convert it to a byte array.
  23.         //string postData = "This is a test that posts this string to a Web server.";
  24.         byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  25.         // Set the ContentType property of the WebRequest.
  26.         request.ContentType = "application/x-www-form-urlencoded";//application/xml;charset=UTF-8
  27.         // Set the ContentLength property of the WebRequest.
  28.         req.ContentLength = byteArray.Length;
  29.         // Get the request stream.
  30.         Stream dataStream = req.GetRequestStream();
  31.         // Write the data to the request stream.
  32.         dataStream.Write(byteArray, 0, byteArray.Length);
  33.         // Close the Stream object.
  34.         dataStream.Close();
  35.         // Get the response.
  36.         WebResponse response = req.GetResponse();
  37.         // Display the status.
  38.         // Console.WriteLine(((HttpWebResponse)response).StatusDescription);
  39.         // Get the stream containing content returned by the server.
  40.         dataStream = response.GetResponseStream();
  41.         // Open the stream using a StreamReader for easy access.
  42.         StreamReader reader = new StreamReader(dataStream);
  43.         // Read the content.
  44.         responseFromServer = reader.ReadToEnd();
  45.         // Display the content.
  46.         Console.WriteLine(responseFromServer);
  47.         // Clean up the streams.
  48.         reader.Close();
  49.         dataStream.Close();
  50.         response.Close();
  51.     } catch (Exception ex) {
  52.         responseFromServer = ex.Message;
  53.     }
  54.     return responseFromServer;
  55. }
  56.  
  57. string endPoint = "";
  58. string postData = JsonConvert.SerializeObject(YourModel);
  59. string responseString = BPJSBridge.HttpPostJSonString($"{endPoint}",postData);
  60. ResponseModel responseModel = JsonConvert.DeserializeObject<ResponseModel>(responseString);
  61.  
  62.  
Tags: C# netcore aspnet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement