【问题标题】:curl command equivalent in javajava中等效的curl命令
【发布时间】:2016-03-04 09:13:11
【问题描述】:

这是我的 curl 命令:

curl https://login.xyz.com/v1/oauth/token -H "Accept:
application/json" --data 'client_id=client_id' --data
'client_secret=client_secret' --data 'redirect_uri=redirect_uri'
--data 'code=code'

我正在尝试在 java 中发布它。这是我想做的:

String resourceUrl = "https://login.xyz.com/v1/oauth/token?client_id=<client.id>&client_secret=<client.secret>&redirect_uri=https://login.xyz.com/user/login&code=<code>";
HttpURLConnection httpcon = (HttpURLConnection) ((new URL(resourceUrl).openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();      
System.out.println(httpcon.getHeaderField(0));

但我收到 HTTP/1.1 500 内部服务器错误

【问题讨论】:

    标签: java post curl


    【解决方案1】:

    我没有测试,只是通过查看文档和您的源代码,我可以看到您的 curl 命令和 Java 实现之间的一些差异:

    卷曲:

    • 执行 POST
    • Content-Type 是 application/x-www-form-urlencoded

    Curl manpage:

    -d,--数据

    (HTTP) 将 POST 请求中的指定数据发送到 HTTP 服务器, 就像浏览器在用户填写 HTML 时所做的一样 表单并按下提交按钮。这将导致 curl 通过 使用内容类型向服务器发送数据 应用程序/x-www-form-urlencoded。与 -F、--form 比较。

    另见:How are parameters sent in an HTTP POST request?

    Java 实现:

    • 执行 POST,但 URL 类似于 GET(您将请求方法设置为 POST,但您在 URL 查询字符串中传递参数)
    • Content-Type 是 application/json

    我希望这会有所帮助。

    【讨论】:

    • 需要 POST:-X/--request &lt;command&gt;, -d - 只是指定数据值的键
    • 不确定我是否同意:superuser.com/questions/149329/…
    • 看来您是对的,-X 仅适用于您想在没有任何数据的情况下发送 POST 的情况
    【解决方案2】:
    public class CURLTest {
        public void main(String[] args) throws IOException {
            sendData();
        }
    
        public String sendData() throws IOException {
            // curl_init and url
    
    
            URL url = new URL( "Put the Request here");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
    
            // CURLOPT_POST
            con.setRequestMethod("POST");
    
            // CURLOPT_FOLLOWLOCATION
            con.setInstanceFollowRedirects(true);
    
            String postData = "my_data_for_posting";
            con.setRequestProperty("Content-length",
                String.valueOf(postData.length()));
    
            con.setDoOutput(true);
            con.setDoInput(true);
    
            DataOutputStream output = new DataOutputStream(con.getOutputStream());
            output.writeBytes(postData);
            output.close();
    
            // "Post data send ... waiting for reply");
            int code = con.getResponseCode(); // 200 = HTTP_OK
            System.out.println("Response    (Code):" + code);
            System.out.println("Response (Message):" + con.getResponseMessage());
    
            // read the response
            DataInputStream input = new DataInputStream(con.getInputStream());
            int c;
            StringBuilder resultBuf = new StringBuilder();
            while ((c = input.read()) != -1) {
                resultBuf.append((char) c);
            }
            input.close();
    
            return resultBuf.toString();
        }
    }
    

    这是一个例子,我会怎么做

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 2019-11-10
      • 1970-01-01
      • 2013-02-02
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      相关资源
      最近更新 更多