简体   繁体   English

我怎样才能在Java中发出这样的Post请求?

[英]How can I do a Post request like this in java?

I have a Post request program by Curl like this. 我有一个Curl这样的Post请求程序。 How can I do exactly this job in java? 我该如何在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"

I use Apache to create a runnable example of POST request 我使用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));
      }
    }
  }

Response from System.out.println() 来自System.out.println()响应

Status: HTTP/1.1 200 OK 状态:HTTP / 1.1 200 OK

response: {"id":4,"token":"QpwL5tke4Pnpja7X4"} 响应:{“ id”:4,“令牌”:“ QpwL5tke4Pnpja7X4”}

Conversion of your code in java 用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());

Hope it helps. 希望能帮助到你。

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

相关问题 编辑:我如何发送带有参数和表单数据的发帖请求,例如JAVA中的此邮递员屏幕截图? - EDIT: How do I send this post Request with parameters AND form-data like this postman screenshot in JAVA? 如何在Http POST请求中发送图像文件? (Java)的 - How do I send an image file in a Http POST request? (java) 如何使用Java发送带有XML数据的HTTPS POST请求 - How do I send an HTTPS POST Request with XML Data in Java 如何在 Java 的 post 请求中添加 cookies? - How do I add cookies in a post request in Java? java/springboot如何取消对POST请求的后端处理? - How can I cancel the backend processing of a POST request in Java/Springboot? 如何在android上使用java发出http post请求 - How can I make an http post request using java on android Spring / Java Web:如何知道请求是表单发布? - Spring/Java web: how can I know a request is a form post? 如何执行返回新页面的 POST 请求 - How can I do POST request that returns a new page 如何将 session 从验证器 POST 请求转发到 JAVA 中的 GET 请求? - How do I take forward a session from a authenticator POST request into a GET request in JAVA? 如何自定义Volley库以将发布请求作为Java对象进行获取以作为Java对象获得响应 - How can I Customize Volley Library to make post request as java object to get response as java Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM