iewi: autumn tree (autumn)
My school has wifi. Free wifi. Bad wifi. Can't even connect half the time wifi. Pretty typical wifi sadly enough. Well, you have to sign in to use the wifi (still don't have secured wifi, but you take what you can get, right? just don't go banking), but anyway it has a web authentication redirect page, because of the many, many IDs they have to keep track of. So far, so good, right? There is one minor annoyance: the web authentication redirect page redirects you to the college's home page once you're done signing in instead of whatever page you were going to. Still, it's pretty ok so far.

I'm coding an android app that requires a web connection to work. As part of making sure it's robust in the exceptions it handles, I'm testing it without any network connection, pointing it to the wrong url, etc. I get this brilliant idea: I'll test it on my college's wifi to see if it can handle a web authentication redirect. I still think it's a brilliant idea, even if it's caused me a lot of headaches recently.

I get the normal exceptions at first, and I handle those. Just pop up a dialog saying "check your network connection" or whatever. Then I start getting weird ones. Null pointer exceptions. XML parser exceptions. I look at my code and 'fix' the null pointer exceptions (hey, it looked fixed at the time). But I'm still getting XML parser exceptions, and I'm confused now. I know the XML is fine; I've tested it before. Web services don't generally just start delivering malformed XML out of the blue. I think, what the hell's wrong here?

I track the flow of control in my program, and I discover that the web authentication page is giving me an HTTP status code 200 OK. This is not cool. It's not cool because a 200 OK means that the request you sent executed like you thought it would and nothing went wrong and you are where you want to be. It doesn't mean that the server served up the code without issue. It means the code served up is the code you want.

So, for example, the New York Times has more than one domain. Let's take nytimes.org for example. Nytimes.org redirects you to the home page of the New York Times, nytimes.com. It gives you a 200 OK, but that's fine. Why? Because if you're going to nytimes.org, you probably want the New York Times. So the server is returning the code you want, so it can give you a 200 OK if everything else works out.

Let's take a different example. Earlier on in the election cycle, back when Jeb Bush was still in the race, someone redirected JebBush.com to Donald Trump's campaign website. In that case, they should give you a HTTP status code in the 300s, because those are the redirects. If you go to JebBush.com, you probably don't expect to get Donald Trump's website, so they shouldn't give you a 200 OK, because you might not have expected that.

The web authentication redirect page at my school should probably have given a 407 Proxy Authentication Required, or maybe something from the 300s. Definitely not a 200 OK. Because my problem ended up being I was expecting XML, but receiving HTML.

And I had even tried to account for that, by having an if statement that checked the response code to see if it was 200 OK, and threw an exception if it wasn't. But my school not sticking with web standards screwed me. Now I check to see if the URL is what I expect it to be.

protected void grabData() { try { HttpURLConnection cnx = (HttpURLConnection) here.openConnection(); cnx.connect();
if (cnx.getResponseCode() == 200 && cnx.getURL().toString().contains("graphical.weather.gov")) in = cnx.getInputStream(); else { throw new IOException("Connection bad: " + cnx.getResponseCode()); } } catch (MalformedURLException e) { networkError = true; e.printStackTrace(); } catch (UnknownHostException e) { networkError = true; e.printStackTrace(); } catch (IOException e) { // you can assume that if the above block of
// code throws an exception, it's a network-
// related one
networkError = true; e.printStackTrace(); } }
In short, THE STANDARDS ARE STANDARDS FOR A REASON. Follow them.

Profile

iewi: feet (Default)
iewi

February 2016

S M T W T F S
 123456
78910111213
14151617181920
2122232425 2627
2829     

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 26th, 2017 12:46
Powered by Dreamwidth Studios