Click here to Skip to main content
11,926,429 members (60,704 online)
Rate this:
Please Sign up or sign in to vote.
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)
                    HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
                catch (WebException e)

When I execute this code here is what i get:

An exception of type 'System.Net.ProtocolViolationException' occurred in but was not handled in user code

I'm really not sure what I need to do here. Any help is greatly appreciated!
Posted 26-Dec-12 11:16am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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 Wink | ;)
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)
            observer =>
                byte[] buffer = new byte[24];
                var obsReadFactory = Observable.Defer(() => stream.AsReader()(buffer, 0, buffer.Length));
                return Observable
                         .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
                             _ =>
                                 if (_.Length > 0)

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";
          .SelectMany(wr => wr.GetResponseStream().ToBytes())
          .ForEach(b => {
                  //Do some stuff with bytes for example get strings 
                  string data=  Encoding.Default.GetString(b);
r83h 27-Dec-12 11:49am
This looks like a great way to do it, however I'm having trouble adding reactive extensions. I have installed from: and then I added a reference to: Reactive Extensions for Windows Phone 8. I have also added "using System.Reactive" at the top of the code file. However it appears I can't just have Iobservable without a type and there is now Observable.FromAsyncPattern. Your help is greatly appreciated!
Oleksandr Kulchytskyi 27-Dec-12 11:53am
What about
using System.Reactive.Linq ??
r83h 27-Dec-12 14:25pm
well I added that and now I get this: The call is ambiguous between the following methods or properties: 'System.Reactive.Linq.Observable.FromAsyncPattern(System.Func, System.Action)' and 'System.Reactive.Linq.Observable.FromAsyncPattern(System.Func, System.Func)'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100