问题
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)
回答1:
Where do you set the header? Here is how to do it:
request.Headers["mycustomheader"] = "abc";
来源:https://stackoverflow.com/questions/13642401/windows-phone-8-http-request-with-custom-header