I'm currently working on an HTTP Request and it worked pretty good so far, now there is an Authentication Window and I'd like to know how to pass Username and Password without adding an external jar like the Appache HTTP Client.
The Basic Authentication with
username:password@exampleUrl.com
didn't work.
My Code so far:
private String[][] content;
public String[][] sendRequest(String urlPart) {
try {
String url = "http://exampleurl.com"+urlPart;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//convert response
JSONObject myresponse = new JSONObject(response.toString());
JSONObject d_object = new JSONObject(myresponse.getJSONObject("d").toString());
JSONArray results = new JSONArray(d_object.getJSONArray("results").toString());
content = new String[results.length()][2];
for (int i = 0; i < results.length(); i++)
{
JSONObject stereo = results.getJSONObject(i);
content[i][0] = stereo.getString("RET_NAME");
content[i][1] = stereo.getString("RET_VALUE");
}
} catch (Exception e) {
e.printStackTrace();
}
return content;
}