1

I am trying to perform a CURL request using Java. The CURL request is as follows:

curl https://apis.sen.se/v2/feeds/N4hSBSpFlYzXT6ZN2IA1KadgSR9rTazv/events/?limit=1 -u username:password

I am trying to perform the request as follows:

String stringUrl = "https://apis.sen.se/v2/feeds/N4hSBSpFlYzXT6ZN2IA1KadgSR9rTazv/events/?limit=1";
URL url = new URL(stringUrl);
URLConnection uc = url.openConnection();

uc.setRequestProperty("X-Requested-With", "Curl");

String userpass = "username" + ":" + "password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);

InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());

and I am trying to see the contents of inputStreamReader as follows:

int data = inputStreamReader.read();
char aChar = (char) data;
System.out.println(aChar);

The code is compiling and running fine, but it is returning nothing. Where am I going wrong?

Colin747
  • 4,695
  • 15
  • 64
  • 108
  • See this question: http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests – inquizitive May 27 '15 at 14:14
  • Is there a temporary user name and password you can provide ? Are there exceptions thrown at any point ? – Deepak Bala May 27 '15 at 14:16
  • There are no exceptions thrown, I'll try to set up temporary access. – Colin747 May 27 '15 at 14:18
  • I went through their site documentation and it asks the dev to first fetch an API key first like so - `curl https://apis.sen.se/v2/user/api_key/ \ -d "username=demoone" \ -d "password=__your_Sen.se_account_password__"`. You're supposed to use that token on your HTTPS requests using `Authorization: Token _Your_Token_`. Have you tried that ? – Deepak Bala May 27 '15 at 14:34

2 Answers2

1

I ended up getting it working using the following code:

public static void main(String args[]) throws IOException {
        String stringUrl = "url";
        URL url = new URL(stringUrl);
        URLConnection uc = url.openConnection();
        uc.setRequestProperty("X-Requested-With", "Curl");
        String userpass = "username" + ":" + "password";
        String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
        uc.setRequestProperty("Authorization", basicAuth);
        StringBuilder html = new StringBuilder();
        BufferedReader input = null;
        try {
          input = new BufferedReader(new InputStreamReader(uc.getInputStream()));
          String htmlLine;
          while ((htmlLine = input.readLine()) != null) {
            html.append(htmlLine);
          }
        }
        catch (IOException e) {
          e.printStackTrace();
        }
        finally {
          try {
            input.close();
          }
          catch (IOException e) {
            e.printStackTrace();
          }
        }
        System.out.println(html.toString());
    }
Colin747
  • 4,695
  • 15
  • 64
  • 108
0

I was also trying to do that thing. I have some kind of workaround but it reads everything it sees.

--Here's the code---

        String params = "some-parameters";
        URL url = new URL("some-website");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(params);
        wr.flush();
        wr.close();
        con.getResponseCode();

        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String line;
        StringBuffer buffer = new StringBuffer();

        while((line = reader.readLine()) != null) {
            buffer.append(line+"\n");
        }
        reader.close();

        System.out.print(buffer.toString());

--Notice, I use this code to see if a certain account exist on a certain website, since it outputs everything, what I do is to find a specific regularity upon the code which could tell me if that user exist or not. Well I'm not really even sure if this could help you, but it might be. Good Luck...