3

I want to send a HTTP PUT request to a WCF server from Windows Phone 8, and for identification I have to send a custom header. (assume "mycustomheader" = "abc")

I was using WebClient so far, but the Webclient.Headers seems not to have an Add method, so it is not possible to send headers other then the ones in HttpRequestHeader enum. Is there any way to do this with WebClient?


I saw it is possible to set a custom header with HttpWebRequest class, but I just can't get it to do anything at all. My test code (basically the sample copied from http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx):

public void dosth()
{
    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://mycomputer/message");
    wr.Method = "PUT";
    wr.ContentType = "application/x-www-form-urlencoded";
    wr.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), wr);
    allDone.WaitOne();
}

private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    Stream postStream = request.EndGetRequestStream(asynchronousResult);
    string postData = "{'Command': { 'RequestType' : 'Status', 'Test' : '1' }}";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    postStream.Write(byteArray, 0, postData.Length);
    postStream.Close();
    request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}

private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
    Stream streamResponse = response.GetResponseStream();
    StreamReader streamRead = new StreamReader(streamResponse);
    string responseString = streamRead.ReadToEnd();
    streamResponse.Close();
    streamRead.Close();
    response.Close();
    allDone.Set();
}

As I can see with wireshark: nothing is arriving at my computer (same url and everything works fine with WebClient .. except for the custom header). In debugging I can see the GetRequestStreamCallback being fired and running through. But it never arrives in the GetResponseCallback. Most stuff I find regarding this refers to methods like GetResponse() that seem not to be available on

Whats is the way to go here? Is it possible to get the HttpWebRequest to work, or is there some workaround to get the custom header set in WebClient or is there even another better way?


edit: webclient code:

WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.ContentLength] = data.Length.ToString();
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.UploadStringAsync(new Uri("http://mycomputer/message"), "PUT", data);

sends the correct data to the correct url. However setting custom header seems not to be possible. (even tried \r\n inside a header ... but this is not allowed and throws exception)

Flo
  • 1,660
  • 4
  • 21
  • 34

1 Answers1

4

Where do you set the header? Here is how to do it:

request.Headers["mycustomheader"] = "abc";
laszlokiss88
  • 4,001
  • 3
  • 20
  • 26
  • 1
    Jea i know that. The problem with the httpWebRequest code is that there is no request arriving at the server whatsoever – Flo Nov 30 '12 at 09:43
  • @Flo - So update your question to reflect you trying to do thattt? – Security Hound Nov 30 '12 at 09:48
  • i thought i made that clear. I want to send a request with customheaders. I tried sending a request with WebClient. It works. But I cant find a way to set custom header there. I tried sending a request with HttpWebRequest because i found that setting a customheader is possible there. Cant get it to work at all. Nothing arriving at my server. The command for setting the header really isnt the problem when I can get it to work. What to do? – Flo Nov 30 '12 at 09:55
  • Must have been some IntelliSense fail, I tried that multiple times and VisualStudio showed errors .. but some time later it did work .. I dont get it, but am happy that it works now. – Flo Dec 07 '12 at 11:56