【问题标题】:How can I do a Post request like this in java?如何在 java 中执行这样的 Post 请求?
【发布时间】:2019-05-08 03:02:09
【问题描述】:

我有一个像这样的 Curl 发布请求程序。我怎样才能在 java 中完成这项工作?

curl -X POST \
-H "api_key: xxxxxxxxxxxxx" \
-H "speed: 0" \
-H "voice: male" \
-H "prosody: 1" \
-H "Cache-Control: no-cache" \
-d 'This is the text to transfer' \
"http://somewhere.com"

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 为什么标记为 PHP?

标签: java curl


【解决方案1】:

你的代码在java中的转换

    String url = ""http://somewhere.com";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("api_key", "xxxxxxxxxxxxx");
    post.setHeader("speed", "0");
    post.setHeader("voice", "male");
    post.setHeader("prosody", "1");
    post.setHeader("Cache-Control", "no-cache");

    post.setEntity(new StringEntity("This is the text to transfer",ContentType.create("text/plain", "UTF-8")));

    HttpResponse response = client.execute(post);
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + post.getEntity());
    System.out.println("Response Code : " + 
                                response.getStatusLine().getStatusCode());

    BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    System.out.println(result.toString());

希望对你有帮助。

【讨论】:

  • 非常感谢。我将此代码应用于 Android 应用程序,但在 android studio 中它说 HttpClient 和 DefaultHttpClient 已被弃用。你知道如何在 Android Studio 中使用这些功能吗?我是 Android Studio 和 Java 的新手。再来一次。谢谢
  • 我不会使用android提供的httpclient,添加依赖{ compile "cz.msebera.android:httpclient:4.4.1.2" } 。并将您的导入更改为 cz.msebera.android.httpclient。更多关于stackoverflow.com/questions/29294479/…
【解决方案2】:

我使用Apache 创建POST 请求的可运行示例

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
...

public static void main(String[] args) throws IOException {
    HttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("https://reqres.in/api/register");

    // Simple data
    httppost.setEntity(
        new StringEntity("{\"email\":\"eve.holt@reqres.in\",\"password\":\"pistol\"}",ContentType.create("application/json", StandardCharsets.UTF_8)));

    // Simple Header
    httppost.setHeader("cache-control", "no-cache");

    // Execute and get the response.
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    System.out.println("Status: " + response.getStatusLine().toString());
    if (entity != null) {
      try (InputStream instream = entity.getContent()) {
        System.out.println("response: " + IOUtils.toString(instream, StandardCharsets.UTF_8));
      }
    }
  }

来自System.out.println()的回复

状态:HTTP/1.1 200 正常

响应:{"id":4,"token":"QpwL5tke4Pnpja7X4"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-28
    • 2022-01-04
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    相关资源
    最近更新 更多