的Windows Phone 8与自定义标题Http请求 [英] Windows Phone 8 Http request with custom header

查看:140
本文介绍了的Windows Phone 8与自定义标题Http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送一个HTTP PUT请求,从Windows Phone 8的一个WCF服务器,识别我必须发送一个自定义标题。 (假设mycustomheader=ABC)

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")

我用 Web客户端到目前为止,但 Webclient.Headers 似乎没有一个添加方法,因此不可能在<$ C发送其他头那么那些$ C> HttpRequestHeader 枚举。有没有什么办法与 Web客户端要做到这一点

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?

我看到它可以设置与的HttpWebRequest 类自定义标题,但我不能得到它做任何事情。我的测试代码(基本上,样品由复制的http:// msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx ):

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();
}



我可以使用Wireshark看到:没有什么是到达我的电脑(相同的URL和一切工作正常 Web客户端 ..除了自定义标题)。在调试我可以看到 GetRequestStreamCallback 被解雇,并贯穿。但它从来没有到达在 GetResponseCallback 。大多数的东西,我找到有关这指的是像的GetResponse),似乎没有可用的

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

请告诉我是要走的路吗?是否有可能获得的HttpWebRequest 来的工作,或者是有一些解决方法,以获取自定义页眉的 WebClient的设置或?是甚至有另一种更好的方法。

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?

编辑:Web客户端代码:

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);



发送正确的数据,以正确的URL。但是设置自定义标题似乎没有可能。 (甚至试图\r\\\
一个头里面的...但是这是不允许的,抛出异常)

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)

推荐答案

在哪里你设置页眉?
这里是如何做到这一点:

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

request.Headers["mycustomheader"] = "abc";

这篇关于的Windows Phone 8与自定义标题Http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆