0

I'm having a problem with a previous app not working on WP8, which works perfectly on WP7.

This is the code I'm using for the http request:

public void SendMessage()
    {    
        request = WebRequest.Create(uri) as HttpWebRequest;
        request.Method = "POST";
        request.AllowReadStreamBuffering = true;
        request.ContentType = "application/octet-stream";

        try 
        {
            // get device info
            String deviceInfo = String.Format("platform,{0};os,{1};width,{2};height,{3};dpi,{4};",
                Config.PLATFORM_NAME,
                Environment.OSVersion.Version.ToString(),
                System.Windows.Application.Current.Host.Content.ActualWidth.ToString(),
                System.Windows.Application.Current.Host.Content.ActualHeight.ToString(),
                96);
            request.Headers["X_MX_DEVICE_INFO"] = deviceInfo;
        } 
        catch (Exception) {}

        request.BeginGetRequestStream(new AsyncCallback(ProcessRequestStream), null);
    }

    private void ProcessRequestStream(IAsyncResult asyncResult)
    {
        if (!message.IsCancelled())
        {
            try
            {
                using (Stream stream = request.EndGetRequestStream(asyncResult))
                {
                    message.GetRequest(stream);
                }
                request.BeginGetResponse(new AsyncCallback(ProcessResponseStream), null);
            }
            catch (Exception e)
            {
                syncContext.Post(OnEnd, e);
            }
        }
        else
        {
            syncContext.Post(OnEnd, null);
        }
    }

    private void ProcessResponseStream(IAsyncResult asyncResult)
    {
        if (!message.IsCancelled())
        {
            try
            {
                response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                if (HttpStatusCode.OK != response.StatusCode)
                {
                    throw new Exception("http status error: " + response.ToString());
                }

                syncContext.Post(SetResponse, response);
            }
            catch (Exception e)
            {
                syncContext.Post(OnEnd, e);
            }
        }
        else
        {
            syncContext.Post(OnEnd, null);
        }
    }

    private void SetResponse(object state)
    {
        Exception ex = null;
        try
        {
            using (Stream stream = ((HttpWebResponse)state).GetResponseStream())
            {
                message.SetRespone(stream);
            }
        }
        catch (Exception e)
        {
            ex = e;
        }
        syncContext.Post(OnEnd, ex);
    }

    private void OnEnd(object state)
    {
        message.OnEnd((Exception)state);
    }
}

It looks as though BeginGetResponse's callback isn't being fired. I've tried Fiddler to see what response is coming back, and looks as though nothing is even coming back, but just for WP8.

Any possible reasons for this?

Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
John Davis
  • 31
  • 1
  • 4
  • try var result=request.GetResponse() to ensure ,also trace it from fiddler if you use emulator. – Davut Gürbüz Mar 27 '13 at 14:28
  • unfortunately my knowledge of fiddler is low as this is the first time iv'e needed it. When using the WP8 emulator there are no logs in fiddler coming back, where as on the WP7 emulator I can see the response in fiddler as expected. var request = .. didn't seem to help the issue.. – John Davis Mar 27 '13 at 14:41
  • Ok. If you code the server side, did you track your query really reach the server side. I see that you post stream as application/octet-stream ,not sure but you don't use JSON? or such a REST service. Can you debug the server side,if you have access? – Davut Gürbüz Mar 27 '13 at 14:47
  • I personally don't code the server side but I could see if it's possible to find out from somebody who does. This is a fairly old app that uses our old binary system, updating to use our json system would take to much resources, so stuck with good old binary – John Davis Mar 27 '13 at 14:54
  • @For server side colleague, you can attach w3wp service and put a breakpoint to service call. When service calls it must hit the breakpoint an by this way you can inspect parameters. If it hits and problem still exist I strongly recommend inspecting tracelog. Hope helps and solves. Please give us the result, by this way we can learn if there is a real problem with WP8. – Davut Gürbüz Mar 27 '13 at 15:00
  • Are any of your exception clauses catching anything? If you set breakpoint and step through the code what happens? Are any exceptions being reported in the output window? – Matt Lacey Mar 27 '13 at 15:25

1 Answers1

0

I believe this could be related to the Referer issue on Windows Phone 8.

For WP7.1: The value of the Referer header is null by default. You can specify a custom value for the Referer header.

For WP8: The value of the Referer header references the installation directory of the app in the format file:///Applications/Install//Install/.

There are some blog posts around the internet, with possible solutions:

But in your case I would still strongly recommend to analyze Fiddler logs. Make sure you have downloaded and installed Fiddler4. Also, make sure you firstly launch fiddler and only then launch the WP emulator.

Mike
  • 549
  • 4
  • 21
  • thanks for your answer. I've looked into this, and the wp7 emulator is sending "file:///Applications/Install/3F43BD1F-735D-433E-B113-67A4E7640BCA/Install/" as it's referer header, which seems to be the opposite? I don't see any response from using the wp8 emulator in fiddler. Isn't there a way to monitor outgoing connection in fiddler to see if it's being sent properly? – John Davis Mar 27 '13 at 16:26
  • Double check the settings for Fiddler. Refer to the guide available on Fiddler website: http://www.fiddler2.com/fiddler/help/phone.asp – Mike Mar 27 '13 at 20:17
  • I've followed the fiddler instructions, more than once now. Fiddler works perfectly for WP7. I'm thinking it could be a different issue causing this. On the WP8 emulator I get warnings like this: The thread 0x658 has exited with code 259 (0x103), which are not there on WP7 emulator. I've googled this with not much luck, what does this error code mean? – John Davis Mar 28 '13 at 10:26
  • Well, cannot say what code 259 stands for. But if the thread has ended with and error code other than 0, means that an exception/runtime error occurred. – Mike Apr 01 '13 at 11:03