现在的宁静。
我有这段代码,它是一个普通的 .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!这根本不是我想要的。我确定我知道 URL 是正确的,那么这里发生了什么?会不会是模拟器的问题?我这里没有设备,所以我无法验证它不是。
我有一种强烈的感觉,有些愚蠢的事情正在发生,因为我知道我在很久以前曾对 WP7 进行过类似的调用。一件非常奇怪的事情是,在提琴手中,对于我的一个错误请求,我只收到隧道请求,并且标头看起来像这样:
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,但 ProtocolVersion 在 WP7 上不可用,因此我无法将其设为 1.1,正如您将在工作请求中看到的那样。当我运行一个工作请求(命令行程序)时,我得到一个 fiddler 隧道条目,如下所示:
CONNECT the.internet.com:443 HTTP/1.1
Host: the.internet.com
Connection: Keep-Alive
然后我得到了我期望的正常请求/响应对。
我如何使它成为 1.1,这甚至重要吗????