Click here to Skip to main content
Rate this: bad
good
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)
            {
                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!
Posted 26-Dec-12 11:16am
r83h471

1 solution

Rate this: bad
good
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)
{
    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
                         );
            }
        );
}
 
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);
                  }));
  Permalink  
v2
Comments
r83h at 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: http://msdn.microsoft.com/en-us/data/gg577610 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 at 27-Dec-12 11:53am
   
What about
using System.Reactive.Linq ??
r83h at 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
0 Maciej Los 205
1 Richard MacCutchan 185
2 DamithSL 170
3 Tomas Takac 146
4 OriginalGriff 132
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2014
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