I have set up in AWS a private API gateway, I can call it use curl like this to access it:
curl -v https://vpce-02e21f9af4f10cf2a-6y6qzepe-ap-southeast-2b.execute-api.ap-southeast-2.vpce.amazonaws.com/Test/uppercase/zzzddd -H'Host:515btqa443.execute-api.ap-southeast-2.amazonaws.com'
I can also use Postman by set the URL and set the header.
However, I seem to have an issue accessing it from Java, I think it is not adding the Header and I get 403 forbidden message.
String urlstr = "https://vpce-02e21f9af4f10cf2a-6y6qzepe-ap-
southeast-2b.execute-api.ap-southeast-
2.vpce.amazonaws.com/Test/uppercase/zzzddd";
URL url = new URL(urlstr);
HttpURLConnection con = (HttpURLConnection)
url.openConnection();
con.setRequestMethod("GET");
con.addRequestProperty("Host", "515btqa443.execute-api.ap-southeast-2.amazonaws.com");
int responseCode = con.getResponseCode();
Any ideas if i am doing something wrong in the Java ?
Related
So I am trying to send a post request to my api using the code below.
When i try the request in fiddler it works. But in android I always get a 400 error.
String url = "http://192.168.1.105:17443/NewUser";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
DataOutputStream os = new DataOutputStream(con.getOutputStream());
String test = user.toJson().toString();
os.writeBytes(test);
os.close();
int responseCode = con.getResponseCode();
Turns out the error I received was a invalid hostname.
I needed to modify my api to allow requests that were not coming from localhost.
you can find more information here:
Connecting to Visual Studio debugging IIS Express server over the lan
I want to read the Network Response after API GET call using
URL obj = new URL("https://URLGoesHere");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
Refer attached image to understand what actually I want to read in Response
after API GET call.
URL url = new URL("http://myserver.com/myendpoint");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//connection.setRequestMethod("POST") <- this didn't help either
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("string=test");
out.close();
connection.close()
The code above WORKS on desktop JVM, sends a post request, parsed on server-side successfully with response 200, however on android, the request method stays GET (yes I checked it IS false) and results in a 404 exception. Official docs say that setting doOutput to true triggers setting the request method to POST but that doesn't seem the case.
404 is not an exception. It is a HTTP status code returned by the server you make the request to and it means that the url you make the request to is not found. That has nothing to do with POST being set or not.
Things to check:
If the url you are making a request to is right.
If the server has a POST controller/handler mapped to the url you are making the request to.
Ask the guy who develops the server if he is handling the cases right ans if he's sending the correct response codes for the relevant scenarios.
Extra info: if the url is registered on the service but a POST request is not allowed you would get a 415 response code.
When posting data to a server, I'm setting some additional request header:
String query = "string=test";
URL url = new URL("http://myserver.com/myendpoint");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
connection.setFixedLengthStreamingMode(query.getBytes("UTF-8").length);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(query);
But as suggested, the 404 exception usually means, that the endpoint, you're trying to access, isn't available.
Try it:
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
I am playing with instagram api endpoints.
Their call sample is:
curl -X DELETE https://api.instagram.com/v1/media/{media-id}/likes?access_token=ACCESS-TOKEN
I am trying to make the call from my android app like this:
URL url = new URL(myBuiltUrl);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestMethod("DELETE");
httpsURLConnection.setDoInput(true);
httpsURLConnection.setDoOutput(false);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpsURLConnection.getOutputStream());
outputStreamWriter.write("access_token=" + token);
outputStreamWriter.flush();
String response = streamToString(httpsURLConnection.getInputStream());
The error I get while running this code is:
java.net.ProtocolException: method does not support a request body: DELETE
How can I fix my code to work correctly ?
Try putting your access_token on your URL, as you are doing with your curl command, and getting rid of the outputStreamWriter stuff.
can someone kindly suggest what I'm doing wrong here?
I'm trying to get the header location for a certain URL using Java
here is my code:
URLConnection conn = url.openConnection();
String location = conn.getHeaderField("Location");
it's strange since I know for sure the URL i'm refering to return a Location header and using methods like getContentType() or getContentLength() works perfectly
Perhaps Location header is returned as a part of redirect response. If so, URLConnection handles redirect automatically by issuing the second request to the pointed resource, so you need to disable it:
((HttpURLConnection) conn).setInstanceFollowRedirects(false);
EDIT:
If you actually need a URL of the redirect target and don't want to disable redirect handling, you may call getURL() instead (after connection is established).
Just a follow up to axtavt's answer... If the url has multiple redirects, you could do something like this in order to obtain the direct link:
String location = "http://www.example.com/download.php?getFile=1";
HttpURLConnection connection = null;
for (;;) {
URL url = new URL(location);
connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
String redirectLocation = connection.getHeaderField("Location");
if (redirectLocation == null) break;
location = redirectLocation;
}