mardi 4 août 2015

Http post and get for json -C#

How can I make an HTTP request and send Json data with Headers and read the response?

The third party had provided some credentials to use their API. Using those credentials I need to invoke the API and read the response. I need to send a header named SIGNATURE along with the request data. The value of the signature is the Encrypted Request data.

I can do the POST request but have no idea how to add the Header.

My code is like this

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("URL");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("SIGNATURE", sEncrypteddata)
using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = "{\"id\":\"2423432432\"," +
                  "\"uid\":\"id123\","+
                  "\"pwd\":\"pass\","+
                  "\"apiKey\":\"2423432432\","+
                  "\"paymentCategory\":0"+
                   "\"paymentType\":0}";

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}
System.Net.ServicePointManager.Expect100Continue = false;
HttpWebResponse  httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    string result = streamReader.ReadToEnd();
}

is this is the correct way ?

Aucun commentaire:

Enregistrer un commentaire