-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();

 
 
 
1
  • Actually, WebRequest is deprecated my Microsoft, use HttpClient instead. Commented Aug 23, 2021 at 8:15

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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();

1 Comment

He wanted to use JSON to send the body so you would have to change the content-type and postdata string
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;
}

Comments

-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

1 Comment

Please add further details to expand on your answer, such as working code or documentation citations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.