我想使用Windows Phone 8模擬器訪問Scoreoid API上的身份驗證。但我在這裏沒有任何成功。我不得不說,儘管Http不是我的事情,並希望有人在這裏可能會發現我的代碼有任何問題!我不斷收到「遠程服務器返回錯誤:NOTFOUND用下面的代碼:WP8 HttpWebRequest總是返回NotFound訪問Scoreoid
堆棧跟蹤:
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at ContosoSocial.OpenXLive.<>c__DisplayClass4.<>c__DisplayClass6.b__2(IAsyncResult state)
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塊該消息:
The remote server returned an error: NotFound.
我在WMAppManifest中啓用了網絡功能,所有地址都被正確定義,並已部署到手機 - 但我很困惑?
您的時間
爲指針非常感謝,我會看看... – TripVoltage 2014-08-29 15:02:09
似乎這在Windows Phone 8 .Net平臺上有點令人頭疼......!我已經忽略了它,並且只用了老的純http://現在正在工作! – TripVoltage 2014-08-29 16:54:00