Windows Phone - https帖子404

时间:2012-05-05 03:53:04

标签: windows-phone-7 https

现在的宁静。

我有这个代码,这是一个普通的.NET命令行应用程序,它可以工作:

var postData = "hello@email.com:MyPassword";
Guid appKey = new Guid("a guid that is a valid app key");
var url = "https://the.internet.com/2.0/Authentication/Basic";
var login = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(postData));

var request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.Headers["Authorization"] = login;
request.Headers["app_key"] = appKey.ToString();
var requestStream = request.GetRequestStream();
requestStream.Write(Encoding.Default.GetBytes("1"), 0, 1);

var response = request.GetResponse();
Console.WriteLine(response.ToString());

好的,那太棒了,我喜欢它。我在控制台上找回了预期的响应。我试图将这个联合移植到WP7,所以我尝试做的是:

public void Authenticate(string username, string password)
{
    Guid appKey = new Guid("a guid that is a valid app key");
    var url = "https://the.internet.com/2.0/Authentication/Basic";

    var request = HttpWebRequest.Create(url) as HttpWebRequest;
    request.Method = "POST";        
    request.Headers["app_key"] = appKey.ToString();
    var postData = "hello@email.com:MyPassword";
    var login = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(postData));
    request.Headers["Authorization"] = login;
    request.BeginGetRequestStream(OpenRequestStream, request);      
}

void OpenRequestStream(IAsyncResult result)
{
    var request = result.AsyncState as HttpWebRequest;
    var stream = request.EndGetRequestStream(result);
    stream.Write(Encoding.UTF8.GetBytes("1"), 0, 1);
    stream.Flush();
    stream.Close();
    request.BeginGetResponse(AuthCallback, request);
}

void AuthCallback(IAsyncResult asyncResult)
{
    HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
    // this line is where i get the 404 exception
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
    using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
    {
        string resultString = streamReader1.ReadToEnd();
        Console.Write(resultString);
    }

    if (Response != null)
        Console.WriteLine("you won the game");
}

当我在WP7模拟器上运行此代码时,我得到了404!这根本不是我想要的。我确定我知道网址是正确的,所以这里发生了什么?它可能是一个模拟器问题吗?我这里没有设备,所以我无法确认它不是。

我有一种强烈的感觉,因为我知道我在遥远的过去曾在WP7上做过类似的调用。一个非常奇怪的事情是,在fiddler中,对于我的一个不良请求,我只收到隧道请求,标题看起来像这样:

CONNECT the.internet.com:443 HTTP/1.0
User-Agent: NativeHost
Host: the.internet.com:443
Content-Length: 0
Connection: Keep-Alive
Pragma: no-cache

我认为这很奇怪,因为它告诉我我们正在使用HTTP 1.0,但是在WP7上没有ProtocolVersion,因此我无法将其设为1.1,因为您将在工作请求中看到它。当我运行一个工作请求(命令行程序)时,我得到一个看起来像这样的fiddler隧道条目:

CONNECT the.internet.com:443 HTTP/1.1
Host: the.internet.com
Connection: Keep-Alive

然后我得到了我期望的正常请求/响应对。

我如何制作1.1并且这甚至是重要的????

1 个答案:

答案 0 :(得分:4)

目前的Windows Phone操作系统有一种令人生气的习惯,即声称404 /" NotFound"对于因各种原因而失败的请求。

为了帮助您继续前进,我能想到的最可能的原因是您的HTTPS证书是自签名的(或者不是来自supported root certificate),因此失败了。如果是这种情况,您需要获得正确的证书,因为这些错误不容忽视。

如果这不是问题,请设置一个断点,以便您可以处理WebException,然后将其Response属性值转换为HttpWebResponse并检查它的属性。如果HttpWebResponse.StatusCode0,则无法建立连接。

还值得检查WebException.Status属性以确定非协议错误(尽管WP7也喜欢声称一切都是Unknown