简体   繁体   English

使用HTTP的Java CURL请求

[英]Java CURL request using HTTP

I am trying to perform a CURL request using Java. 我正在尝试使用Java执行CURL请求。 The CURL request is as follows: CURL请求如下:

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: 并且我试图查看inputStreamReader的内容,如下所示:

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? 我要去哪里错了?

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());
    }

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... 祝好运...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM