WP8 HttpWebRequest访问Scoreoid时总是返回NotFound
本文关键字:返回 NotFound HttpWebRequest 访问 Scoreoid WP8 | 更新日期: 2023-09-27 18:19:56
我正在尝试使用Windows Phone 8 Emulator访问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.AncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态)位于System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult-asyncResult)位于ContosoSocial.OpenXLive。<>c_DisplayClass4。<>c_DisplayClass6.b_2(IAsyncResult状态)
信息:
远程服务器返回错误:NotFound。
我在WMAppManifest中启用了网络功能,所有地址都定义正确,并已部署到手机上,但我很困惑?
非常感谢您抽出时间
在页面上https://api.scoreoid.com/v1/getPlayer存在证书问题,我认为您的NotFound错误是由此引起的。Windows Phone不允许访问具有无效/不受信任证书的网站。
也许在开发Windows Phone 8.1 XAML(WinRT,而不是Silverlight)应用程序时,可以禁用此检查:
使用HttpClient 允许不受信任的SSL证书