-2

i, tried putting body in request but didn't actually worked, in body i want to put which is in json format {"ClaimNo":"123123"}

i have used this as code:

    string ClaimStatus_url  = "https:xyz";
     WebRequest request = WebRequest.Create(ClaimStatus_url);
     request.ContentType = "application/json";
     request.Method = "POST";
     //request.Headers = "";// i used this for puting body in it but did not work
     WebResponse response = request.GetResponse();
     Stream responseStream = response.GetResponseStream();
     StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
     string result = reader.ReadToEnd();

 
 
 
  • Actually, WebRequest is deprecated my Microsoft, use HttpClient instead. – Ivan Khorin Aug 23 '21 at 08:15
  • 1
    Does this answer your question? [How to post JSON to a server using C#?](https://stackoverflow.com/questions/9145667/how-to-post-json-to-a-server-using-c) – cassandrad Aug 23 '21 at 12:49

4 Answers4

3
using System.Text;
using System.Text.Json;

namespace TestPostData;

public class Data
{
    public int ClaimNo { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var postData = new Data
        {
            ClaimNo = 123123,
        };

        var client = new System.Net.Http.HttpClient();

        var content = new StringContent(JsonSerializer.Serialize(postData), Encoding.UTF8, "application/json");

        var response = client.PostAsync("https:xyz", content).Result;
    }
}

That is an example of using HttpClient class that is now recommended to use instead WebRequest.

Ivan Khorin
  • 827
  • 1
  • 5
  • 17
0

Try this, i hope it will work.

 var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");

var postData = "thing1=" + Uri.EscapeDataString("hello");
    postData += "&thing2=" + Uri.EscapeDataString("world");
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  • He wanted to use JSON to send the body so you would have to change the content-type and postdata string – jallmen Aug 23 '21 at 13:56
0

I would start off by using RestSharp.

dotnet add package RestSharp

Then I would create a DTOS object with the contents that you want to send in the json:

public class DtosObject
{
    public string ClaimNo {get; set;}
}

Then pass that in as the object (I would call the class something relevant to the data it contains). If you only are using ClaimNo you could also use a KeyValuePair like this:

var body = new KeyValuePair<string, string>("ClaimNo", "123123");

Then you can send requests like this:

public async Task<IRestResult> PostAsync(string url, object body)
{
    var client = new RestClient(url);
    client.Timeout = -1;

    var request = new RestRequest(Method.Post);
    request.AddJsonBody(body);

    var response = await client.ExecuteAsync(request);
    return response;
}
jallmen
  • 136
  • 1
  • 7
-1
                  string ClaimStatus_url  = "https://qms.xyz.in/FGClaimWsAPI/api/Common/FetchClaimdetails";

                                        var httpWebRequest = (HttpWebRequest)WebRequest.Create(ClaimStatus_url);
                                        httpWebRequest.ContentType = "application/json";
                                        httpWebRequest.Method = "POST";
                                        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                                        {
                                            string json = "{\"ClaimNo\":\""+ userProfile.ClaimNumber +"\"}";
                                            //string json = "{\"ClaimNo\":\"CV010831\"}";
                                            //await turnContext.SendActivityAsync(MessageFactory.Text(json, json), cancellationToken);
                                            streamWriter.Write(json);
                                        }
                                        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                                        var result1 = "" ;
                                        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                                        {
                                            var result = streamReader.ReadToEnd();
                                            result1 = result.Substring(1, result.Length -2); // to bring response result in proper format
                                                                                             

                                        }

                                        _claimstaus = GenrateJSON_Claim(result1);

This upper code worked