HttpWebRequest BeginGetResponse callback not firing on WP8 (working on WP7)

删除回忆录丶 提交于 2019-12-08 12:33:20

问题


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?


回答1:


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:

  • http://geoffwebbercross.blogspot.nl/2012/01/wp7-http-using-sockets-problem-with.html
  • http://atomaras.wordpress.com/category/windows-phone-8/

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.



来源:https://stackoverflow.com/questions/15660741/httpwebrequest-begingetresponse-callback-not-firing-on-wp8-working-on-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!