C# WP8 HttpWebRequest始终返回NotFound访问Scoreoid
我正在尝试使用Windows Phone 8模拟器访问Scoreoid API上的身份验证。但我在这里没有任何成功。我必须说,虽然Http不是我的东西,我希望这里的人可能会发现我的代码有任何错误!我一直收到“远程服务器返回错误:NotFound,代码如下:C# WP8 HttpWebRequest始终返回NotFound访问Scoreoid,c#,windows-phone-8,httpwebrequest,C#,Windows Phone 8,Httpwebrequest,我正在尝试使用Windows Phone 8模拟器访问Scoreoid API上的身份验证。但我在这里没有任何成功。我必须说,虽然Http不是我的东西,我希望这里的人可能会发现我的代码有任何错误!我一直收到“远程服务器返回错误:NotFound,代码如下: string baseUri = "https://api.scoreoid.com/v1/getPlayer"; HttpWebRequest webRequest = (HttpWebReques
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.BeginNoui(SendOrPostCallback beginMethod,对象状态)
位于System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
在ContosoSocial.OpenXLive.c_uuudisplayClass4.c_uudisplayClass6.b_uu2(IAsyncResult状态)
信息:
远程服务器返回错误:NotFound
我在WMAppManifest中启用了网络功能,所有地址都被正确定义并部署到了手机上——但我很困惑
非常感谢您抽出时间在页面上出现证书问题,我认为您的NotFound错误是由此引起的。Windows Phone不允许访问具有无效/不受信任证书的站点
可能在开发Windows Phone 8.1 XAML(WinRT,而非Silverlight)应用程序时,可以禁用此检查:
在Windows Phone 8.Net平台上,这似乎有点让人头疼…!我已经推荐了s,并且只使用了旧的普通http://版本,该版本目前还在使用中!