Windows Phone 8 Http request with custom header

懵懂的女人 提交于 2020-01-24 10:09:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!