Ok so I'm working on an app with 2 activities.
Let's call the 1st one LoginActivity which appears on top of my MainActivity. Also, I have a class SenderReceiver which extends AsyncTask that takes care of the connection to my https server.
The 1st time I call SenderReceiver from my LoginActivity, everything works fine & I get a 200 response & I'm able to use the JSON retrieved.
After that, the LoginActivity finishes & returns a result to the MainActivity. This is when I know I'm logged in & I can proceed.
Now whenever I execute SenderReceiver to get other information, I get the 403 response with a java.io.FileNotFoundException: https://url_of_my_server. But if I call the login again, it works.
Below is the SenderReceiver code snippet which connects to the server.
I'm also connecting to the same server on the iOS version of the app & never have issues with subsequent calls after the login.
Now this is my 1st time using https connections & such, I'm not really sure how it works on Android so I must be missing something or doing something wrong.
#Override
protected Boolean doInBackground(Object... params) {
frag = (Fragment)params[0]; // For later
addedParams = (List<NameValuePair>)params[1];
postPage = (String)params[2];
InputStream is = null;
try {
URL url = new URL(Constants.kWebService);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setReadTimeout(15000);
conn.setConnectTimeout(20000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
//conn.setDoInput(true);
conn.setRequestProperty("CONTENT_TYPE", "application/json");
conn.setRequestProperty("X-requested-with", "XMLHttpRequest");
String base64EncodedCredentials = "Basic " + Base64.encodeToString((Constants.kHTTPSUser+":"+Constants.kHTTPSPass).getBytes(), Base64.NO_WRAP);
conn.setRequestProperty("Authorization", base64EncodedCredentials);
String param = "page="+postPage+"&";
for(int i = 0; i < addedParams.size(); i++) {
String and = (i < addedParams.size()-1)? "&": "";
param += addedParams.get(i).getName()+"="+ URLEncoder.encode(addedParams.get(i).getValue(), "UTF-8")+and;
}
conn.setFixedLengthStreamingMode(param.getBytes().length);
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.close();
// Starts the query
conn.connect();
int responseCode = conn.getResponseCode();
is = conn.getInputStream();
// Convert the InputStream into a string
responseSTR = Constants.inputStreamToString(is).toString();
return true;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} catch (ProtocolException pe) {
Constants.Log("Protocol Exception:"+pe.getMessage());
} catch (IOException io) {
Constants.Log("IO Exception:"+io.getMessage());
io.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException io) {
Constants.Log("is.close IO Exception:"+io.getMessage());
}
}
}
return false;
}
Fixed it!
All I had to do was to set the cookie header with:
conn.setRequestProperty("Cookie", Constants.kCookies);
& retrieve that cookie after the response in order to set it with:
List<String> cookies = conn.getHeaderFields().get("Set-Cookie");
if(cookies != null) {
Constants.kCookies = cookies.get(0);
}
That way in the initial HttpURLConnection connections (i.e:login) the cookie is empty & not needed. Once a connection is successful (after "login") it receives the cookie & sets it.
After that, any subsequent connection sends that cookie as part of the header.
The updated method below:
#Override
protected Boolean doInBackground(Object... params) {
frag = (Fragment)params[0];
addedParams = (List<NameValuePair>)params[1];
postPage = (String)params[2];
InputStream is = null;
try {
URL url = new URL(Constants.kWebService);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setReadTimeout(15000);
conn.setConnectTimeout(20000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
//conn.setDoInput(true);
conn.setRequestProperty("CONTENT_TYPE", "application/json");
conn.setRequestProperty("X-requested-with", "XMLHttpRequest");
String base64EncodedCredentials = "Basic " + Base64.encodeToString((Constants.kHTTPSUser+":"+Constants.kHTTPSPass).getBytes(), Base64.NO_WRAP);
conn.setRequestProperty("Authorization", base64EncodedCredentials);
// Get the cookie from my Constants file & set it, Constants.kCookies is a static String
conn.setRequestProperty("Cookie", Constants.kCookies);
String param = "page="+postPage+"&";
for(int i = 0; i < addedParams.size(); i++) {
String and = (i < addedParams.size()-1)? "&": "";
param += addedParams.get(i).getName()+"="+ URLEncoder.encode(addedParams.get(i).getValue(), "UTF-8")+and;
}
conn.setFixedLengthStreamingMode(param.getBytes().length);
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.close();
// Starts the query
conn.connect();
int responseCode = conn.getResponseCode();
Constants.Log("The response code is: " + responseCode);
is = conn.getInputStream();
// Retrieve the cookie from the response & if not null save it to Constants.kCookies
List<String> cookies = conn.getHeaderFields().get("Set-Cookie");
if(cookies != null) {
Constants.kCookies = cookies.get(0);
}
// Convert the InputStream into a string
responseSTR = Constants.inputStreamToString(is).toString();//readIt(is, len);
return true;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} catch (ProtocolException pe) {
Constants.Log("Protocol Exception:"+pe.getMessage());
} catch (IOException io) {
Constants.Log("IO Exception:"+io.getMessage());
io.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException io) {
Constants.Log("is.close IO Exception:"+io.getMessage());
}
}
}
return false;
}
Related
I'm passing a URL to an ASYNC class (wsbRedirTest) and I expect to receive the content of the page. However, it does not return any results.
public class wsbRedirTest extends AsyncTask<String, Void, String> {
private Object[] values;
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... par1) {
try {
String myUrl;
myUrl=par1[0];
URL obj = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
System.out.println("Request URL ... " + myUrl);
boolean redirect = false;
// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
System.out.println("Response Code ... " + status);
if (redirect) {
// get redirect url from "location" header field
String newUrl = conn.getHeaderField("Location");
// get the cookie if need, for login
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
System.out.println("Redirect to URL : " + newUrl);
}
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
System.out.println("URL Content... \n" + html.toString());
System.out.println("Done");
return html.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
I believe the problem is here. I'm probably not calling it properly. But I did several tests and did not get the result I wanted. I am calling from MainActivity.
String par = "www.google.com";
String content = new wsbRedirTest().execute(par);
First off- AsyncTask is deprecated. Don't use it. Even when it wasn't, it was a bad choice for web requests (despite how many examples you see using it) as due to how they work on a single thread any other asynctask would be blocked on that request.
Secondly- that's not how asynchronous code works. It's asynchronous. It doesn't return a value, because it won't know the value to return until much later. Any code that needs the value you want it to return should instead be put in onPostExecute. The execute function doesn't return the result of your operation, it returns an instance of the AsyncTask itself.
When I Debug my code get Response code 200 which means success. Then also I'm getting null response.
Following is my AsyncTask Class:
private class AsyncAddfriend extends AsyncTask<String, String, String> {
HttpURLConnection conn;
URL url = null;
#Override
protected String doInBackground(String... params) {
try {
url = new URL("http://ishook.com/users/friends/send_friend_request_json/");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("sessionId", params[0])
.appendQueryParameter("UserId", params[1])
.appendQueryParameter("friendId", params[2]);
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return(result.toString());
}else{
return("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return "exception";
} finally {
conn.disconnect();
}
}}
I have Tested My API in postman its Working with response code 200 and giving Response in json format but in my code its not working .
Hope you will understand my problem.
Thank you very much for your time and assistance in this matter.
The problem is probably from this line:
String query = builder.build().getEncodedQuery();
You need to use:
String query = builder.build().toString();
This is because getEncodedQuery() is only returning the query, from the documentation:
String getEncodedQuery ()
Gets the encoded query component from this URI. The query comes after the query separator ('?') and before the fragment separator ('#'). This method would return "q=android" for "http://www.google.com/search?q=android".
UPDATED
You're building the query after opening the connection, hence you having the error.
You need to build the url with the query first:
Uri uri = Uri.parse("http://ishook.com/users/friends/send_friend_request_json/")
.buildUpon()
.appendQueryParameter("sessionId", params[0])
.appendQueryParameter("UserId", params[1])
.appendQueryParameter("friendId", params[2]);
.build();
URL url = new URL(builtUri.toString());
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
Note: I haven't test the code. So, don't expected it working automagically.
I don't know what is wrong with my code I keep getting error 401 when I try making a request to the GitHub. My app uses the REST API before now I and to convert it to the Graphql but I am finding it difficult
private static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
// If the URL is null, then return early.
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Authorization","Bearer token");
urlConnection.setRequestProperty("Content-Type", "application/json");
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
wr.writeBytes("{\"query\":\"query{search(type:USER query:\"location:lagos language:java\"){userCount}}}");
wr.flush();
wr.close();
int rc = urlConnection.getResponseCode();
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
Found the mistake
There was a problem with my token and the query format was wrong it should have been
"{\"query\": \"query { search ( type : USER, query : \\\"location:lagos\\\" ) { userCount }}\"}"
Thank for your suggestion
The Authorization header token might not be valid. HTTP 401 = not authorized.
I'd suggest trying to make same request with a Curl and when you see success - apply same parameters/headers to HttpUrlConnection.
I'm trying to use HttpURLConnection for connecting to server from Android app which I'm developing. For now, I'm testing the connection code not in an app but as a plain java program with main class. I guess this doesn't make any difference as far as HttpUrlConnection.
Please examine the code snippet. Another issue is even errorStream is throwing null. This I feel is because of malformed URL.
private static String urlConnectionTry() {
URL url; HttpURLConnection connection = null;
try {
String urlParameters = "email=" + URLEncoder.encode("email", "UTF-8") +
"&pwd=" + URLEncoder.encode("password", "UTF-8");
//Create connection
url = new URL("http://example.com/login");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("uuid", getUuid());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getErrorStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
private static String getUuid() {
try {
Document doc=Jsoup.connect("http://example.com/getUuid").get();
Elements metaElems = doc.select("meta");
for (Element metaElem : metaElems) {
if(metaElem.attr("name").equals("uuid")) {
return metaElem.attr("content");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
You're probably receiving 401 because the credentials that was sent to the server is not authorized- it's probably not registered or the password is incorrect.
As for the null error stream, take a look at this SO answer.
If the connection was not connected, or if the server did not have an error while connecting or if the server had an error but no error data was sent, this method will return null.
It is probably better if you check first the response code using HttpUrlConnection#getResponseCode(). Decide on whether you'll be checking the contents of the error stream based on the response code you get.
I am using restheart to provide a restful interface to mongodb. The interface is set up and running and provides the correct answer if a GET request is sent through Chrome. However if I use the following java code using a HttpURLConnection I get a 201 response with no content.
try {
videos = new URL("http://www.example.com:8080/myflix/videos");
} catch (Exception et) {
System.out.println("Videos URL is broken");
return null;
}
HttpURLConnection hc = null;
try {
hc = (HttpURLConnection) videos.openConnection();
String login="admin:admin";
final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);
final String encoded = Base64.getEncoder().encodeToString(authBytes);
hc.addRequestProperty("Authorization", "Basic "+encoded);
hc.setDoInput(true);
hc.setDoOutput(true);
hc.setUseCaches(false);
hc.setRequestMethod("GET");
hc.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
hc.setRequestProperty("Content-Type", "application/json");
hc.setRequestProperty("Accept", "application/json,text/html,application/hal+json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*");
} catch (Exception et) {
System.out.println("Can't prepare http URL con");
return (null);
}
BufferedReader br = null;
try {
OutputStreamWriter writer = new OutputStreamWriter(
hc.getOutputStream());
} catch (Exception et) {
System.out.println("Can't get reader to videos stream");
}
String inputLine;
String sJSON = null;
try {
int rc = hc.getResponseCode();
What is the correct way to authenticate using Java to the resthert interface? (Details on the restheart authentication is here Restheart authentication
I made few changes (look for inline comments starting with <==) and it works:
The way you generate the authentication request header is correct. When I run your code I actually got 415 Unsupported Media Type, that went away commenting out hc.setDoOutput(true). A GET is a input operation, in fact you were also trying to get an OutStream from the connection: you need to get an InputStream actually.
URL url;
try {
url = new URL("http://127.0.0.1:8080/test/huge");
} catch (Exception et) {
System.out.println("Videos URL is broken");
Assert.fail(et.getMessage());
return;
}
HttpURLConnection hc = null;
try {
hc = (HttpURLConnection) url.openConnection();
String login = "admin:admin";
final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);
final String encoded = Base64.getEncoder().encodeToString(authBytes);
hc.addRequestProperty("Authorization", "Basic " + encoded);
System.out.println("Authorization: " + hc.getRequestProperty("Authorization"));
hc.setDoInput(true);
//hc.setDoOutput(true); <== removed, otherwise 415 unsupported media type
hc.setUseCaches(false);
hc.setRequestMethod("GET");
hc.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
hc.setRequestProperty("Accept", "application/json,text/html,application/hal+json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*");
} catch (Exception et) {
System.out.println("Can't prepare http URL con");
}
System.out.println(hc.toString());
BufferedReader br = null;
try {
InputStreamReader reader = new InputStreamReader(hc.getInputStream()); // <== the request is a GET, data is in input
} catch (Exception et) {
System.out.println("Can't get reader to videos stream");
}
int rc = hc.getResponseCode();
System.out.println("response code: " + rc);
System.out.println("response message: " + hc.getResponseMessage());
Assert.assertEquals(200, rc);