Calling Rest API in java from Hackerrank IDE - java

I was trying to call an api like below. It works in my IDE(Intellij), where as it fails in hackerrank IDE while getting input stream.
import java.io.*;
import java.util.*;
import java.net.*;
URL url = new URL("https://jsonmock.hackerrank.com/api/article_users?page=1");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.addRequestProperty("Content-Type", "application/json");
InputStream is = connection.getInputStream();
Getting an error:
error while calling: java.net.UnknownHostException: jsonmock.hackerrank.com
inside hacker rank IDE.
Please let me know your suggestions

just remove s from https
URL url = new URL("http://jsonmock.hackerrank.com/api/article_users?page=1");

Are you sure Hackerrank IDE has api call feature?
Perhaps for security or some other reason, this feature may be blocked or not supported.

It may be the case that Hackerrank does not let Java code make outbound network requests. Similar questions here and here suggest that you may need to choose a different language that Hackerrank supports. I imagine they should list it somewhere on their site, or on the IDE page directly.

Related

How to capture response of a Bean shell sampler

Above is my code i have written to hit a web service whose endpoint is expecting a Byte Stream object.
I am able to do that but i am not getting any response.
I have to test the response.
Though i am getting 200 ok but a string is sent in response that i am not getting.
And the response is blank
How can I get the response ?
In order to read server's response you need to use URLConnection.getInputStream() method, not OutputStream
In order to convert stream to string you can use IOUtils.toString() method
In order to return data you can use return keyword
Minimal working code is below, adjust as per your needs:
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
URL url = new URL("http://example.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
String response = IOUtils.toString(con.getInputStream(), StandardCharsets.UTF_8);
return response;
Be aware of fact that JMeter is built on top of Apache HttpComponents so you can use the power of these libraries in order to create HTTP requests, see QuickStart wiki page to get ramped up in minutes
Be aware that starting from JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for scripting, check out Apache Groovy - Why and How You Should Use It for comprehensive explanation, benchmarks, code samples, etc.
You can add your output by using the SampleResult object:
String output = "...";
SampleResult.setResponseData( output );
SampleResult.setDataType( org.apache.jmeter.samplers.SampleResult.TEXT );

HttpClient ignoring encoding, on a single computer

I am using HttpClient (version 3.1) on several different (but apparently identical) computers to read a UTF-8 encoded JSON data from a URL.
On all the machines, save one, it works fine. I have some Spanish language words and they come through with accents and tildes intact.
One computer stubbornly refuses to cooperate. It is apparently treating the data as ISO-8859-1, despite a Content-Type: application/json;charset=utf-8 header.
If I use curl to access that URL from that computer, it works correctly. On every other computer, both curl and my HttpClient-based program work correctly.
I did an md5sum on the common-httpclient.jar file on each machine: the same.
Is there some setting, deep in Linux, that might be different and be messing with me? Any other theories, or even places to look?
EDIT: some people asked for more details.
Originally I had the problem deep in the bowels of a complex Tomcat app, but I lightly adapted the sample to just retrieve the URL in question, and (fortunately) had the same problem.
These are Linux 2.6 machines running jdk1.7.0_45.
An env command yields a bunch of variables. The only one that looks remotely on point is LANG=en_US.UTF-8.
How do you get the json response data from HttpClient?
If you get it back in binary form (through getResponseBodyAsStream() for example), and then convert it to a String without specifying charset, then the result depends on your JVM's default charset.
You can check the value of JVM default charset by:
Charset.defaultCharset().name()
This might give "UTF-8" on all machines except the one failing.
Without seeing your code, it is difficult to say what's wrong, but here is a "correct" way of doing this (using HttpClient 3.1.0 for request and Jackson 2.1.3 to parse the JSON).
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.HttpStatus;
import java.io.IOException;
import java.io.InputStreamReader;
HttpClient hc = new HttpClient();
GetMethod get = new GetMethod(uri);
int status = hc.executeMethod(get);
if (status != HttpStatus.SC_OK) throw new RuntimeException("http status " + status);
ObjectMapper jsonParser = new ObjectMapper(new JsonFactory());
// we use an InputStreamReader with explicit charset to read the response body
JsonNode json = jsonParser.readTree(
new InputStreamReader(get.getResponseBodyAsStream(), get.getResponseCharSet())
);
I already faced this issue and this was because of the encoding type configured in the client. So I had to make a "work around" like the one below:
String encmsg = new String(respStr.getBytes("ISO-8859-1"), java.nio.charset.Charset.forName("UTF-8"));
It reads the String as ISO-8859-1 and convert to UTF-8.

Weird problem accessing web page with Java

I am trying to write a program that reads the html source code of the website http://judgephilosophies.wikispaces.com. I wrote some simple java code that reads and outputs the source code, but it just prints out "null." Here's the bizarre thing, though - if I replace "http://judgephilosophies.wikispaces.com" in the code with any other website, it works just fine. It only seems to be for websites in the wikispaces.com domain that the program doesn't work, and I am utterly befuddled as to why. The code is below. Help is much appreciated.
import java.io.*;
import java.net.*;
public class AccessWebExample
{
public static void main (String[] args) throws Exception
{
//Create reader to access html source code
URL url = new URL ("http://judgephilosophies.wikispaces.com/");
InputStreamReader isr = new InputStreamReader (url.openStream());
BufferedReader reader = new BufferedReader (isr);
//Read and print the text
do
{
System.out.println(reader.readLine());
}
while(reader.readLine() != null);
}
}
Do an HTTP trace using Wireshark or somesuch and compare. It's probably a matter of cookies or headers, if the bare URLConnection is acting differently than a browser.
Using wget from the command line you'll find:
broach#broach-laptop:~$ wget http://judgephilosophies.wikispaces.com/
--2011-04-23 14:50:31-- http://judgephilosophies.wikispaces.com/
Resolving judgephilosophies.wikispaces.com... 208.43.192.33, 75.126.104.177
Connecting to judgephilosophies.wikispaces.com|208.43.192.33|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://session.wikispaces.com/1/auth/auth?authToken=e8ad55c0e2701a0e7da89807255609da [following]
It redirects (a couple more times, actually). Your bare URLConnection doesn't handle that. The response code is in the headers so your program currently prints null.
You really should look at using HttpUrlConnection as it can handle redirects for you. To do it with URL would require you looking at the returned headers and acting on HTTP response codes (which is what HttpURLConnection does)

Using java class HttpsURLConnection

I have a small piece of code which basically impements a HTTP-Client,
i.e. it POSTS request and works with re RESPONSE. As long as HTTP is
concenerned everthing work well. For some reason I now have to support
HTTPS too. So here is briefly what I do in order to get a connection opened:
URL url = new URL(serverAddress);
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
This fails, stating:
sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to com.sun.net.ssl.HttpsURLConnection
I guess this is kinda trivial, but I just don't get what I'm doing wrong in this one...
Googled it, and the code just looks right - not?
any ideas are appreciated!
Just keep it java.net.URLConnection or cast it to java.net.HttpURLConnection instead. Both offers methods to do the desired task as good.
A side remark unrelated to the technical problem: you should never explicitly import/use Sun Java SE implementation specific classes in your code. Those are undocumented classes and are subject to changes which may cause your code break when you upgrade the JVM. On the other hand, your code may also break when you run it at a different brand JVM.
Update: since you seem to accidentally have imported it, go to Window > Preferences > Java > Appearance > Type Filters and Add com.sun.* and sun.* to the list. This way you won't ever import them accidentally:
Your url's protocol should also be https and not http. Check your url.
The above problem is only caused by two issues
Using wrong import
Using http in the string you create url from use instead https
Instead of creating a URL object using standard constructor like
URL wsURL = new URL(url);
Use
java.net.URL wsURL = new URL(null, url,new sun.net.www.protocol.https.Handler());
which would solve this problem
Check your imports, you should be using
java.net.HttpURLConnection
or
javax.net.ssl.HttpsURLConnection
Check value of your "serverAddress" variable. It should https and not http
Change:
import javax.net.ssl.HttpsURLConnection;
and
URL url = new URL(serverAddress);
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
Change To:
import java.net.HttpURLConnection;
and
URL url = new URL(serverAddress);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
Hard to tell without seeing the whole file, but it looks like you're importing com.sun.net.ssl.HttpsURLConnection when you really want javax.net.ssl.HttpsURLConnection.
In my case, the protocol and port were not correct while invoking the httpsUrlConnection.
Port and protocol were defined as static class variables. And the step prior to the failed step, was invoking an httpUrlConnection. That method changed the port/protocol to 80/http, but didn't set it back to /https at the end. So eventhough httpsUrlConnection was invoked, it was still using http/80. Once I reset those at the end of the httpUrlConnection method, the error disappeared.

Implementing Java's getResponseCode() in C?

If it's any help, there is also a similar class in C#'s WebRequest. Although I do not want it in java or .NET, i am wondering how to implement this in native C/C++ code (for windows).
for reference:
try {
URL url=new URL("http://google.ca");
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.connect();
int code = con.getResponseCode();
System.out.println(code);
} catch (MalformedURLException e) {
System.err.println("Error reading URL.");
}
prints out:
200
meaning "OK"
I understand I probably need to use sockets and send a User-Agent string, but I haven't a clue where to begin. Whenever I learn a new language the first thing I like to do is try porting my code to it, but this one has stumped me.
Any help is appreciated
There is no HTTP support in standard C library.
So you have two options - use 3rd-party HTTP library, such ar libcurl, or handle HTTP yourself:
open socket
resolve hostname
connect to server
build HTTP request
send request to server
receive HTTP response
parse response and get response code from it.

Categories