# 401 message return on Java but OK on browser

**URL:** <https://discussions.flightaware.com/t/401-message-return-on-java-but-ok-on-browser/15464>\
**Category:** AeroAPI\
**Created:** [September 21, 2014, 12:09am UTC](https://discussions.flightaware.com/t/401-message-return-on-java-but-ok-on-browser/15464 "2014-09-21T00:09:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hexa](https://avatars.discourse-cdn.com/v4/letter/h/ed655f/32.png) [@hexa](https://discussions.flightaware.com/u/hexa)\
**Post date:** [September 21, 2014, 12:09am UTC](https://discussions.flightaware.com/t/401-message-return-on-java-but-ok-on-browser/15464/1 "2014-09-21T00:09:32Z")

</div>

Hi there, I’m trying to use the JSON feature with the flightXML2.0. When I build the URL (below) and request it via a web browser - it works (that is - I get a valid reply with valid data).

```auto

http://hexa:MY_KEY@flightxml.flightaware.com/json/FlightXML2/MetarEx?airport=KJFKK&startTime=0&howMany=1&offset=0

```

When testing under Java I get the notorious 401 message

```auto

URL url = new URL("http://hexa:MY_KEY@flightxml.flightaware.com/json/FlightXML2/MetarEx?airport=KJFK&startTime=0&howMany=1&offset=0");

		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setRequestProperty("Accept", "application/json");

```

Exception in thread “main” java.io.IOException: Server returned HTTP response code: 401 for URL:

```auto

http://hexa:MY_KEY@flightxml.flightaware.com/json/FlightXML2/MetarEx?airport=KJFKK&startTime=0&howMany=1&offset=0
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1459)
	at com.flightaware1.RunTest1.main(RunTest1.java:23)

```

Any idea why I can’t get the reply just as if I post it in a web browser? Many thanks!

---

<div class="post-metadata">

**Author:** ![bovineone](https://sea1.discourse-cdn.com/flex019/user_avatar/discussions.flightaware.com/bovineone/32/117631_2.png) [@bovineone](https://discussions.flightaware.com/u/bovineone)\
**Post date:** [September 23, 2014, 6:32pm UTC](https://discussions.flightaware.com/t/401-message-return-on-java-but-ok-on-browser/15464/2 "2014-09-23T18:32:03Z")

</div>

HTTP 401 means you have not supplied the Authorization header in the request with your API credentials. You must use HTTP Basic Auth with your username and your FlightXML API key as your password.

---

<div class="post-metadata">

**Author:** ![hexa](https://avatars.discourse-cdn.com/v4/letter/h/ed655f/32.png) [@hexa](https://discussions.flightaware.com/u/hexa)\
**Post date:** [September 24, 2014, 2:36pm UTC](https://discussions.flightaware.com/t/401-message-return-on-java-but-ok-on-browser/15464/3 "2014-09-24T14:36:44Z")

</div>

I’m pasting here a java snapshot of my code with your pointer. Still get the 401 error. Wonder if you can assist what I’m doing wrong. Thanks

```auto

	public static void test2() throws Exception
	{
		try {
			String webPage = "http://flightxml.flightaware.com/json/FlightXML2/MetarEx?airport=KJFK&startTime=0&howMany=1&offset=0";
			String name = "hexa";
			String password = "MY_PASSWORD";

			String authString = name + ":" + password;
			System.out.println("auth string: " + authString);
			byte] authEncBytes = Base64.encodeBase64(authString.getBytes());
			String authStringEnc = new String(authEncBytes);
			System.out.println("Base64 encoded auth string: " + authStringEnc);

			URL url = new URL(webPage);
			URLConnection urlConnection = url.openConnection();
			urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
			InputStream is = urlConnection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);

			int numCharsRead;
			char] charArray = new char[1024];
			StringBuffer sb = new StringBuffer();
			while ((numCharsRead = isr.read(charArray)) > 0) {
				sb.append(charArray, 0, numCharsRead);
			}
			String result = sb.toString();

			System.out.println(" ***BEGIN***");
			System.out.println(result);
			System.out.println(" ***END***");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

```

---

<div class="post-metadata">

**Author:** ![bovineone](https://sea1.discourse-cdn.com/flex019/user_avatar/discussions.flightaware.com/bovineone/32/117631_2.png) [@bovineone](https://discussions.flightaware.com/u/bovineone)\
**Post date:** [September 25, 2014, 4:45am UTC](https://discussions.flightaware.com/t/401-message-return-on-java-but-ok-on-browser/15464/4 "2014-09-25T04:45:32Z")

</div>

Make sure you are using your FlightXML API key and not your website password for “MY\_PASSWORD”

The code looks fine otherwise.
