WP8 HttpWebRequest总是返回NotFound访问Scoreoid

时间:2014-08-29 13:43:02

标签: c# windows-phone-8 httpwebrequest

我正在尝试使用Windows Phone 8模拟器访问Scoreoid API上的身份验证。但我在这里没有任何成功。我不得不说虽然Http不是我的事情,并希望有人在这里可能发现我的代码有任何问题!我一直得到"远程服务器返回错误:NotFound,带有以下代码:

             string baseUri = "https://api.scoreoid.com/v1/getPlayer";
        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(baseUri);
        webRequest.Method = "POST";
        // What we are sending
        string postData = String.Format("api_key={0}&game_id={1}&response={2}&username={3}",
                HtmlEncode(apiKey),
                HtmlEncode(gameID),
                HtmlEncode("XML"),
                HtmlEncode(name));

        // Turn our request string into a byte stream
        byte[] postBuffer = Encoding.UTF8.GetBytes(postData);
        // This is important - make sure you specify type this way
        webRequest.ContentType = "application/x-www-form-urlencoded";
        int timeoutInterval = 30000;
        DateTime requestDate = DateTime.Now;
        Timer timer = new Timer(
        (state) =>
        {
            if ((DateTime.Now - requestDate).TotalMilliseconds >= timeoutInterval)
                webRequest.Abort();
        }, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(1000));
        //webRequest.ContentLength = postBuffer.Length;
        //webRequest.KeepAlive = false;
        //webRequest.ProtocolVersion = HttpVersion.Version10;
        try
        {
            webRequest.BeginGetRequestStream(
            requestAsyncResult =>
            {
                try
                {
                    HttpWebRequest request = ((HttpWebRequest)((object[])requestAsyncResult.AsyncState)[0]);
                    byte[] buffer = ((byte[])((object[])requestAsyncResult.AsyncState)[1]);
                    Stream requestStream = request.EndGetRequestStream(requestAsyncResult);
                    requestStream.Write(buffer, 0, buffer.Length);
                    requestStream.Close();

                    requestDate = DateTime.Now;

                    request.BeginGetResponse((state) =>
                    {
                        timer.Change(Timeout.Infinite, Timeout.Infinite);
                        HttpWebResponse response = null;
                        try
                        {
                            Debug.WriteLine("Before call to response = HttpWebResponse: ");
                            response = (HttpWebResponse)((HttpWebRequest)state.AsyncState).EndGetResponse(state);
                            Debug.WriteLine("HttpWebResponse: " + response);
                            if (response.StatusCode == HttpStatusCode.OK)
                            {
                                // If the request success, then call the success callback
                                // or the failed callback by reading the response data
                                using (Stream stream = response.GetResponseStream())
                                {
                                    try
                                    {
                                        XDocument xdoc = XDocument.Load(stream);
                                        // Data contains error notification.
                                        if (xdoc.Root.Name == "error")
                                            throw new InvalidOperationException(xdoc.Root.Value);
                                        //success(xdoc);
                                    }
                                    catch (Exception ex)
                                    {
                                        //failed(ex.Message);
                                    }
                                    stream.Close();
                                }
                            }
                            else
                            {
                                // If the request fails, then call the failed callback
                                // to notfiy the failing status description of the request
                                //failed(response.StatusDescription);
                                Debug.WriteLine("Exception: " + response);
                            }
                        }
                        catch (Exception ex)
                        {
                            // If the request fails, then call the failed callback
                            // to notfiy the failing status description of the request
                            //failed("Unknown HTTP error.");
                            Debug.WriteLine("Exception1: " + ex.Source);
                        }
                        finally
                        {
                            request.Abort();
                            if (response != null)
                                response.Close();
                        }
                    }, request);
                }
                catch (Exception ex)
                {
                    // Raise an error in case of exception
                    // when submitting a request
                    //failed("Unknown HTTP error.");
                    Debug.WriteLine("exception2 " + ex.Message);
                }
            }, new object[] { webRequest, postBuffer });
        }
        catch (Exception ex)
        {
            // Raise an error in case of exception
            // when submitting a request
            //failed("Unknown HTTP error.");
            Debug.WriteLine("Exception3 " + ex.Message);
        }
    }

代码总是在exception1块中以及以下内容结束:

堆栈跟踪:

  

在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state)      在System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)      在ContosoSocial.OpenXLive。<> c__DisplayClass4。<> c__DisplayClass6.b__2(IAsyncResult状态)

消息:

  

远程服务器返回错误:NotFound。

我在WMAppManifest中启用了网络功能,所有地址都已正确定义并已部署到手机 - 但我感到困惑?

非常感谢你的时间

1 个答案:

答案 0 :(得分:1)

在页面https://api.scoreoid.com/v1/getPlayer上存在证书问题,我认为您的NotFound错误是由此引起的。 Windows Phone不允许访问具有无效/不受信任证书的站点。

也许在开发Windows Phone 8.1 XAML(WinRT,而非Silverlight)应用时,可以禁用此检查:

Allowing Untrusted SSL Certificates with HttpClient