0

I am provided a webservice url similar to: http://ericdev35:7280/persons/persons/ and a username and password.

I want to make a post call on this web service from WPF application. The data to be sent to the service is the first name and last name of a person in the format: "fname=Abc&lname=Xyz"

How can I make a call for this in C#? Here is the code that I have tried:

        HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create("http://ericdev35:7280/persons/persons/");
        httpWebRequest.Method = "POST";
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Credentials = new NetworkCredential(username, password);
        string data = "fname=Abc&lname=Xyz";
        StreamWriter writer = new StreamWriter(httpWebRequest.GetRequestStream());
        writer.Write(data);
        writer.Close();

This does not give me any error but I cannot see the data that I have posted. Is there anything that needs to be corrected? Is the Content Type correct?

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
The King
  • 833
  • 1
  • 15
  • 41

1 Answers1

0

This Method posts json. After that it gets the response and deserialize the Json Object.

private static string PostJson<T1>(string p_url, string p_json, string p_method, out T1 p_target)
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(p_url);
    httpWebRequest.UseDefaultCredentials = true;
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = p_method;

    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        streamWriter.Write(p_json);
        streamWriter.Flush();
        streamWriter.Close();
    }
    HttpWebResponse httpResponse;
    try
    {
        httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    }
    catch (WebException ex)
    {
        httpResponse = ex.Response as HttpWebResponse;
    }

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var a_result = streamReader.ReadToEnd();

        //If you dont need a Json object delete anything behind here
        try
        {
            p_target = JsonConvert.DeserializeObject<T1>(a_result);
        }
        catch { p_target = default(T1); }
        return a_result;
    }
}
RoJaIt
  • 451
  • 3
  • 10
  • How should I pass my data as json. Is the following statement correct ? string data = "fname=Abc&lname=Xyz"; – The King Jun 27 '17 at 07:09
  • string data = "{\"fname\":\"Abc\", \"lname\":\"Xyz\"}"; – RoJaIt Jun 27 '17 at 07:10
  • I passed it as: string data = "{fname:" + "\"" + "Abc" + "\"" + ",lname:" + "\"" + "Xyz" + "\"}"; The response that I get is 403 Forbidden error – The King Jun 27 '17 at 07:18
  • Did you try to look at the full response? It may contain a more detailed error description. – RoJaIt Jun 27 '17 at 07:32
  • httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); This line gives a 403 forbidden exception. I tried catching this WebException and the status property of the exception says "Protocol Error" – The King Jun 27 '17 at 07:34
  • The `try catch` part of my methode provides you the complete response if the status is not `Ok`. – RoJaIt Jun 27 '17 at 07:37
  • You could try http and https. Depending on the configuration of the webservice one is may not accepted. – RoJaIt Jun 27 '17 at 07:39