如何将OkHttp的代码转换为使用Apache Http?

问题描述 投票:0回答:2

我正在努力获得一个帖子请求以在我的项目中返回 200 个答案。我们有一个 Postman 测试,运行良好并生成以下代码:

OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("customerNumber","1000000")
  .addFormDataPart("username","razisami")
  .addFormDataPart("password","razisami1234")
  .build();
Request request = new Request.Builder()
  .url("http://localhost:8085/ImageGateway/login")
  .method("POST", body)
  .build();
Response response = client.newCall(request).execute();
'''
I need the same code written again to work with Apache HttpClient, see [Posting with HttpClient][1]


  [1]: https://www.baeldung.com/httpclient-post-http-request
java okhttp apache-httpclient-4.x
2个回答
2
投票

这是与 Apache HttpClient 5.1 等效的代码,我建议在新项目中使用它来代替 HttpClient 4.5

CloseableHttpClient httpclient = HttpClients.createSystem();
ClassicHttpRequest request = ClassicRequestBuilder.post()
        .setUri(new URI("http://localhost:8085/ImageGateway/login"))
        .addParameter("customerNumber","1000000")
        .addParameter("username","razisami")
        .addParameter("password","razisami1234")
        .build();
httpclient.execute(request, response -> {
    // do something useful with the response
    return null;
});

0
投票

如何转换 HTTPUrlConnection 或 HttpPost??? 我可以使用 PostMan 上传文件,邮递员代码如下,但无法使用 httpUrlconnection httpPost 上传文件。请帮助我。下面的代码是我上传文件时来自邮递员的。我如何从 HttpUrlConnection 或 httpPost 上传文件??

© www.soinside.com 2019 - 2024. All rights reserved.