【问题标题】:Windows Phone 8 Http request with custom header带有自定义标头的 Windows Phone 8 Http 请求
【发布时间】:2012-11-30 09:34:44
【问题描述】:

我想从 Windows Phone 8 向 WCF 服务器发送一个 HTTP PUT 请求,并且为了识别我必须发送一个自定义标头。 (假设“mycustomheader”=“abc”)

到目前为止,我一直在使用WebClient,但Webclient.Headers 似乎没有Add 方法,因此除了HttpRequestHeader 枚举中的标题之外,无法发送标题。有没有办法用WebClient 做到这一点?


我看到可以使用HttpWebRequest 类设置自定义标题,但我根本无法让它做任何事情。我的测试代码(基本上是从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 并且WebClient 一切正常......除了自定义标题)。在调试中,我可以看到GetRequestStreamCallback 被触发并运行。但它永远不会到达GetResponseCallback。我发现的大多数关于此的东西都是指像 GetResponse() 这样的方法,这些方法似乎在

上不可用

去这里的路是什么?是否可以让HttpWebRequest 工作,或者是否有一些解决方法可以在WebClient 中设置自定义标头,或者还有其他更好的方法吗?


编辑:网络客户端代码:

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\n ...但这是不允许的并引发异常)

【问题讨论】:

标签: c# http httpwebrequest webclient windows-phone-8


【解决方案1】:

你在哪里设置标题? 操作方法如下:

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

【讨论】:

  • 我知道。 httpWebRequest 代码的问题是没有任何请求到达服务器
  • @Flo - 所以更新您的问题以反映您正在尝试这样做吗?
  • 我想我已经说得很清楚了。我想发送一个带有自定义标头的请求。我尝试使用 WebClient 发送请求。有用。但我找不到在那里设置自定义标题的方法。我尝试使用 HttpWebRequest 发送请求,因为我发现在那里设置自定义标头是可能的。根本无法让它工作。什么都没有到达我的服务器。当我可以让它工作时,设置标题的命令真的不是问题。怎么办?
  • 一定是某些 IntelliSense 失败了,我尝试了多次,VisualStudio 显示错误.. 但过了一段时间它确实工作了.. 我不明白,但很高兴它现在可以工作。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2018-02-09
  • 1970-01-01
  • 2014-08-08
  • 1970-01-01
相关资源
最近更新 更多