thebeekeeper
thebeekeeper

Reputation: 364

Windows Phone - https post 404

Serenity Now.

I have this code, which is a plain-old .NET command line app, and it works:

var postData = "[email protected]: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());

Ok, that's great, and I like it. I get back the expected response on the console. I'm trying to port this joint over to WP7 however, so what I try to do is:

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 = "[email protected]: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");
}

When I run this code on the WP7 emulator I get a 404! That's not what I want at all. I'm certain I know the URL is correct, so what's going on here? Could it be an emulator issue? I don't have a device here, so I can't verify that it's not.

I have a strong feeling something dumb is going on since I know I've made similar calls on WP7 in the distant past. One very strange thing is that in fiddler, for one of my bad requests I only get the tunnel request, and the header looks like this:

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

I think it's weird because it's telling me we're doing HTTP 1.0, but ProtocolVersion is not available on WP7, so I can't make it 1.1 as you'll see in the working request. When I run a working request (the command line program) I get a fiddler tunnel entry that looks like this:

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

And then I get the normal request/response pair that I expect.

How do I make it 1.1 and does that even matter????

Upvotes: 1

Views: 883

Answers (1)

Richard Szalay
Richard Szalay

Reputation: 84744

The current Windows Phone OS has an infuriating habit of claiming 404 / "NotFound" for requests that fail for a variety of reasons.

In the interest of helping you move on, the most likely reason I can think of is that your HTTPS certificate is self signed (or not from a supported root certificate) and is therefore failing. If this is the case, you'll need to get a proper certificate as these errors cannot be ignored.

If that's not the issue, set a breakpoint so you can handle the WebException, then cast it's Response property value to an HttpWebResponse and examine it's properties. If the HttpWebResponse.StatusCode is 0, then a connection could not be made.

It's also worth checking the WebException.Status property to determine non-protocol errors (though, again, WP7 likes to claim everything is Unknown)

Upvotes: 4

Related Questions