简体   繁体   English

将特定的cUrl请求转换为Java /与Java

[英]Translating specific cUrl request into/with Java

I had an issue: I am trying to convert a curl request into java. 我遇到了一个问题:我正在尝试将curl请求转换为java。 Thusfar, I have tried the makeCurl JAR files as well as tried directly using Apache http. 因此,我尝试了makeCurl JAR文件,并尝试直接使用Apache http。 However, I can't figure out the conversion. 但是,我无法弄清楚转换。 I am new to both Java and Curl , so I know most of the issue is a lack of familiarity with the API. 我是JavaCurl新手,所以我知道大部分问题都是缺乏对API的熟悉。 However, as this is time sensitive, I would really appreciate the help turning this relatively simple line of code into java. 但是,由于这是时间敏感的,我真的很感激帮助将这个相对简单的代码行转换为java。

curl \
  -H 'Authorization: Bearer O3YPXEJHQ6TWNFI7HIBEGLVDBZA2SEKN' \
  'https://api.wit.ai/message?q=my_message'

Which returns: 哪个回报:

{

  "msg_id" : "e0d5c96d-ca14-43a8-bcf9-993e5c717155",

  "msg_body" : "my_message",

  "outcome" : {
    "intent" : "greetings",
    "entities" : { },
    "confidence" : 0.525

  }

If I could get this return back into a string, I could finish the rest of the program. 如果我能把这个返回到一个字符串中,我可以完成剩下的程序。 This one step has been a pain though. 这一步虽然很痛苦。 Thank you so much in advance for the help! 非常感谢您的帮助!

-Andrew -安德鲁

Update: 更新:

Okay: I can now send the request, and it is being received by the server, though I am using the entirely wrong code. 好的:我现在可以发送请求了,但服务器正在接收它,尽管我使用的是完全错误的代码。 I tried the java.net.URL/URLConnection library, and used this: 我尝试了java.net.URL / URLConnection库,并使用了这个:

package curlHttp;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class curlStackTest {

    public static void main(String args[]) throws IOException {
        String stringUrl = "https://api.wit.ai/message?q=test";
        URL url = new URL(stringUrl);
        URLConnection uc = url.openConnection();

        uc.setRequestProperty("Authorization", "Bearer O3YPXEJHQ6TWNFI7HIBEGLVDBZA2SEKN");


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

        // read this input

    }
}

Now i just need to figure out how to read the output coming back from the cUrl request... 现在我只需要弄清楚如何读取从cUrl请求返回的输出...

Try with the following code 请尝试使用以下代码

Scanner scanner = new Scanner(inputStreamReader);

while(scanner.hasNextLine()){
    System.out.println(scanner.nextLine());
}

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

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