19

I am not really sure what type of headers these highlighted values are, but how should I add them using HttpWebRequest?

HTTP Header

Is the highlighted part considered body of the http request or header data? In other words, which way is correct?

Here is the code I am currently using:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("/securecontrol/reset/passwordreset");
request.Headers.Add("Authorization", "Basic asdadsasdas8586");
request.ContentType = "application/x-www-form-urlencoded";
request.Host = "www.xxxxxxxxxx.com";
request.Method = "POST";
request.Proxy = null;
request.Headers.Add("&command=requestnewpassword");
request.Headers.Add("&application=netconnect");

But should I use the following instead to build the Http Request above?

string reqString = "&command=requestnewpassword&application=netconnect";
byte[] requestData = Encoding.UTF8.GetBytes(reqString);

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("/securecontrol/reset/passwordreset");
request.Headers.Add("Authorization", "Basic ashAHasd87asdHasdas");
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestData.Length;
request.Proxy = null;
request.Host = "www.xxxxxxxxxx.com";
request.Method = "POST";

using (Stream st = request.GetRequestStream())
st.Write(requestData, 0, requestData.Length);
3
  • Your first code snippet should work fine, but to verify that the data is in fact in the header, did you try looking at the packet in Wireshark or something similar? In Wireshark, you can right-click a packet, select to open it in a new window, then expand the "Hypertext Transfer Protocol" section. Commented Mar 23, 2012 at 16:12
  • @M. Babcock: They are not the same, I changed the values in the code. Commented Mar 23, 2012 at 16:13
  • @Lander : I am working with a very secure server and the actual code is so big and it seems it takes time to test it unfortunately :/ Commented Mar 23, 2012 at 16:51

2 Answers 2

23

A simple method of creating the service, adding headers and reading the JSON response,

private static void WebRequest()
{
    const string WEBSERVICE_URL = "<<Web Service URL>>";
    try
    {
        var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
        if (webRequest != null)
        {
            webRequest.Method = "GET";
            webRequest.Timeout = 20000;
            webRequest.ContentType = "application/json";
            webRequest.Headers.Add("Authorization", "Basic dcmGV25hZFzc3VudDM6cGzdCdvQ=");
            using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                {
                    var jsonResponse = sr.ReadToEnd();
                    Console.WriteLine(String.Format("Response: {0}", jsonResponse));
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! This is exactly what I needed for my header. Thank you.
Glad to help @Vippy
This works great but you in my case I needed to do the following: myHttpWebRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password")));
13

IMHO it is considered as malformed header data.

You actually want to send those name value pairs as the request content (this is the way POST works) and not as headers.

The second way is true.

2 Comments

OK thanks I agree with you I just wanted to make sure I am doing fine.
While this might not be best practice, sending headers in an HTTP request is sometimes necessary, particularly when using certain APIs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.