带有https的Windows Phone 8 Webrequest和使用Get的自定义标头 [英] Windows Phone 8 Webrequest with https and custom header using Get
问题描述
嘿大家我正在编写Windows手机8,它是客户端iOS应用程序的副本。我需要能够发送带有自定义标头条目的get请求。使用Windows 8这很容易,但使用Windows PHONE 8我遇到了麻烦。
以下是我所拥有的:
Hey everyone I''m in the process of writing a Windows phone 8 that is a copy of a clients iOS application. I need to be able to send a get request with custom header entries. Using Windows 8 this is easy, however using Windows PHONE 8 I have been having trouble.
Here is what i have:
WebRequest myClient = WebRequest.Create(_devApiUrl + "session/" + _entityKey);
myClient.Headers["Host"] = _host;
myClient.ContentType = "text/json";
myClient.Method = "GET";
myClient.BeginGetResponse(ReadWebRequestCallback, myClient);
然后只是想得到一个回复我有这个:
and then just trying to get a response I have this:
private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
if (myRequest != null)
{
try
{
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
}
catch (WebException e)
{
return;
}
}
}
当我执行此代码时,我得到的是:
When I execute this code here is what i get:
An exception of type ''System.Net.ProtocolViolationException'' occurred in System.Windows.ni.dll but was not handled in user code
我真的不确定我需要做什么。非常感谢任何帮助!
I''m really not sure what I need to do here. Any help is greatly appreciated!
推荐答案
嗨=)
*首先,你必须更仔细地处理异步模式! />
*按照你的方式编码,如何正确地说 - 是旧学校;)
但是,让我们解决你的问题,我要解决它借助称为Rx的第三方工具(WP的反应性扩展)。
所以,让我们定义一些辅助方法:
Hi =)
*First of all you must deal with Async pattern more carefully !!
* Coding the way like you did , how correct to say - is old school ;)
However , let''s fix your problem,i''m going to resolve it with help of third-party tool called Rx (Reactive extension for WP).
So, let''s define few helper methods :
public static IObservable ToResponse(this WebRequest request)
{
var asyncGetResponse = Observable.FromAsyncPattern(
request.BeginGetResponse, request.EndGetResponse);
return Observable.Defer(asyncGetResponse);
}
public static IObservable ToBytes(this Stream stream)
{
return
Observable.Create(
observer =>
{
byte[] buffer = new byte[24];
var obsReadFactory = Observable.Defer(() => stream.AsReader()(buffer, 0, buffer.Length));
return Observable
.Repeat(obsReadFactory)
.Select(i => buffer.Take(i).ToArray())
// Subscribe on the thread pool, otherwise the repeat operator will operate during the
// call to subscribe, preventing the whole expression to complete properly
.SubscribeOn(Scheduler.ThreadPool)
.Subscribe(
_ =>
{
if (_.Length > 0)
{
observer.OnNext(_);
}
else
{
observer.OnCompleted();
}
},
observer.OnError,
observer.OnCompleted
);
}
);
}
剩下的最后一件事就是处理WebRequest及其正确的请求
And the last thing that remain , is to deal with WebRequest and proper request for it
string url = _devApiUrl + "session/" + _entityKey;
var webRequest = WebRequest.Create(url);
webRequest.Headers["Host"] = _host;
webRequest.ContentType = "text/json";
webRequest.Method = "GET";
webRequest.ToResponse()
.SelectMany(wr => wr.GetResponseStream().ToBytes())
.ForEach(b => {
//Do some stuff with bytes for example get strings
string data= Encoding.Default.GetString(b);
}));
这篇关于带有https的Windows Phone 8 Webrequest和使用Get的自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!