Http Request for API - java

I am learning to how to use http request. The following is the code which return JSON. All I am doing is to get it and print it. But I am facing some error. The error is also given below.
import java.io.*;
import java.net.*;
public class ZipTester {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
URL stck = new URL("http://www.zipfeeder.us/zip?key=Ect9O9ta&zips=14623");
URLConnection yc = stck.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String add="";
while ((inputLine = in.readLine()) != null) {
//System.out.println(inputLine);
add=add+inputLine;
}
in.close();
System.out.println(add);
}
}
This is the error. In my old machine this code worked perfectly fine. I just got a new machine this week. The same code is now not working. previously I was using jdk 1.7 and now i am using jdk 1.8
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.zipfeeder.us/zip?key=Ect9O9ta&zips=14623
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at ZipTester.main(ZipTester.java:11)

You can fix your code with adding user-agent header like this :
yc.addRequestProperty("User-Agent",""); //to avoid 403 error
The website had probably change it access rules and now it request a "user-agent".
The full code :
import java.io.*;
import java.net.*;
public class ZipTester {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
URL stck = new URL("http://www.zipfeeder.us/zip?key=Ect9O9ta&zips=14623");
URLConnection yc = stck.openConnection();
yc.addRequestProperty("User-Agent",""); //to avoid 403 error
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String add="";
while ((inputLine = in.readLine()) != null) {
add=add+inputLine;
}
in.close();
System.out.println(add);
}
}
I hope it will help you.
Regards.

I fixed this issue by setting the user agent with a following value:
yc.setRequestProperty("User-Agent","Mozilla/5.0");

Related

Getting "SocketException Connection Reset" while invoking Jenkins API

I have a sample Java Program which connects to a particular Jenkins URL connection and calls one API. The program was working few days ago, but suddenly it has started showing SocketException Connection Reset for all the calls. I am unable to figure out what configuration has gone for a change in Jenkins Config.
Note: The below piece of code is working for all other URLs except this Jenkins setup. What could have gone wrong? Even I am able to call the API via postman.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static String getReq(String URL, String serviceName) {
StringBuffer response = new StringBuffer();
try {
URL obj = new URL(
"https://jenkins.abcdef.com/cid/jobs/abc/config.xml");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET"); //$NON-NLS-1$
con.setRequestProperty("User-Agent", "Mozilla/5.0"); //$NON-NLS-1$ //$NON-NLS-2$
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return response.toString();
}
public static void main(String[] args) {
getReq(null, null);
}
}
Seems that you are missing a:
con.connect();
right before this line:
int responseCode = con.getResponseCode();

Eclipse Kura http authentication failed

I am trying to do a simple HTTP Get and POST request in java to my Eclipse Kura gateway but i dont know how to authenticate using username and password. I tried using the url syntax http://user:pw#ipaddress:port/ but i still get HTTP error code 401.
import java.io.*;
import java.net.*;
public class HTTP {
public static String getHTML() throws Exception {
StringBuilder result = new StringBuilder();
String urlToRead = "http://user:pw#ipaddress:port";
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
public static void main(String[] args) throws Exception {
System.out.println(getHTML());
}
}
I believe that you are not providing the credentials in the desired way. This very similar question already has an accepted answer, in which James Van Huis suggests using java.net.Authenticator for setting authentication data prior to opening any connections.

how to use HttpsUrlConnection in java

I have a command line:
curl -u username:passwd -k "https://myserver/a/b/c"
It works and I will get right information when I call it under Ubuntu or Cygwin.
Now I am willing to accomplish it in Java. So I have Java code like that:
public class Test {
public static String auth = "username:passwd";
public static String url = "https:/myserver/a/b/c";
public static void main(String[] args) {
try {
URL url = new URL(url);
final byte[] authBytes = auth.getBytes(StandardCharsets.UTF_8);
String encoding = java.util.Base64.getEncoder().encodeToString(authBytes);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", encoding);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
But It seems not work and always return HTTP response code 411.
Is there something wrong with the java code?
Thanks in advanced.
HTTP response code 411 means that "The server refuses to accept the request without a defined Content- Length."
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
HttpsUrlConnection should be able to do that for you. Check out setFixedLengthStreamingMode(). I think you will also need to setDoOutput().

Response code 500 when accesing rest based web services though eclipse

I am trying to invoke a web service from an Eclipse program. But I am getting response code 500 as response. When I access the URL from IE or Mozilla it works fine for me. My sample Eclipse program:
public static void main(String[] args) {
// Use apache commons-httpclient to create the request/response
HttpClient client = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("aaa", "aaaa");
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
GetMethod method = new GetMethod("http://localhost:8080/usersByID/2039");
try {
client.executeMethod(method);
InputStream in = method.getResponseBodyAsStream();
// Use dom4j to parse the response and print nicely to the output stream
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
System.out.println(out.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
When I opened the link in IE or Mozilla I am getting exact output. The credentials are correct.
Can any one help to overcome this.
Thanks.

GWT Error: "The output stream has been committed and can no longer be written to"

New to Java and GWT. Here is my problem. I am getting this error message: "The OutputStream has been committed and can no longer be written to." while I am trying to post xml to a remote server via REST API.
It is happening on this line in the code below:
out.write("Content-Type: application/x-www-form-urlencoded\r\n");
This works from terminal:
curl -d "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=DxxxxxxxxxxxxxxxxxxxB6&INPUT_DATA=<?xml version=%221.0%22 encoding=%22utf-8%22?><Operation><Details><requester>Me</requester><subject>Test</subject><description>Testing curl input again</description></Details></Operation>" http://app.company.com/sdpapi/request/
I'm having trouble translating the above curl command into the code below. I got the following code from a tutorial and I'm not sure how to pass in the URL and parameters properly. Any suggestions or additional methods of troubleshooting will be appreciated. I did not find much of anything on google for this.
package com.gwt.HelpDeskTest.server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.gwt.HelpDeskTest.client.HelpDeskTestService;
import com.gwt.HelpDeskTest.shared.HelpDeskTestException;
#SuppressWarnings("serial")
public class HelpDeskTestImpl extends RemoteServiceServlet implements
HelpDeskTestService {
#Override
public String getFromRemoteServer(String serviceUrl)
throws HelpDeskTestException {
String result = "";
try {
final URL url = new URL(serviceUrl);
final BufferedReader in= new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine= in.readLine()) != null) {
result+= inputLine;
}
in.close();
return result;
} catch (final Exception e) {
throw new HelpDeskTestException();
}
}
#Override
public String postToRemoteServer(String serviceUrl)
throws HelpDeskTestException {
try {
final String serverHost= "http://app.company.com/";
final String serverPath= "http://app.company.com/sdpapi/request/";
final String serverParameters=
"OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=Dxxxxxxxxxx6&INPUT_DATA=%3C?xml%20version=%25221.0%2522%20encoding=%2522utf-8%2522?%3E%3COperation%3E%3CDetails%3E%3Crequester%3EMe%3C/requester%3E%3Csubject%3ETest%3C/subject%3E%3Cdescription%3ETesting%20GWT%20input%20again%3C/description%3E%3C/Details%3E%3C/Operation%3E"; //put parameters here for testing.
final URL url = new URL(serverHost);
final URLConnection connection= url.openConnection();
connection.setDoOutput(true);
connection.setConnectTimeout(10000); //added this to see if I can address the timeout issue.
connection.setReadTimeout(10000);
final OutputStreamWriter out= new OutputStreamWriter(connection.getOutputStream());
final BufferedReader in= new BufferedReader(new InputStreamReader(
connection.getInputStream()));
out.write("POST " + serverPath + "\r\n");
out.write("Host: " + serverHost + "\r\n");
out.write("Accept-Encoding: identity\r\n");
out.write("Connection: close\r\n");
out.write("Content-Type: application/x-www-form-urlencoded\r\n"); //This is where the error is occuring
out.write("Content-Length: " + serverParameters.length() + "\r\n\r\n" +
serverParameters + "\r\n");
String result = "";
String inputLine;
while ((inputLine=in.readLine()) != null) {
result+= inputLine;
}
in.close();
out.close();
return result;
} catch (final Exception e) {
System.out.println(e.getMessage());
throw new HelpDeskTestException();
}
}
}
I think it would be easier for you to use an HttpURLConnection instead of a plain URLConnection. It has convenience methods to set the headers and other properties. See the accepted answer to this question for a good example of how to use it to do a POST:
Java - sending HTTP parameters via POST method easily

Categories

Resources