当前位置: 首页 > 知识库问答 >
问题:

在java中发出HttpClient请求

方弘
2023-03-14

我正在创建一个数据下载程序,自从使用httpClient以来,它一直给我带来问题

此代码将所需的所有变量附加到URL:

   private void createOpenSession() {
   String params = new StringBuilder()
           .append("?u=").append(this.user)
           .append("&p=").append(this.password)
           .append("&q=").append(this.qualifier)
           .toString();
   this.openSession = new HttpGet(this.target + params);

}

每当我调用openSession时,它都会以一种简单的方式打印出标题(这正是我需要它做的)。。。

   public void start() throws ClientProtocolException, IOException  {

        try {
            HttpResponse response = this.httpClient.execute(this.openSession);
            if (response.getStatusLine().getStatusCode() == 200) {
                try {
                    String responseContent = this.readResponse(response);
                    System.out.println(responseContent);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                }}
            finally {
               createCloseSession();
            }
        }

这打印了一个单行这个家伙: ool e3r,但当我提出另一个打印长行的请求时,它总是带来这个错误:

    Exception in thread main
java.lang.IllegalArgumentException: HTTP request may not be null
    at org.apache.http.util.Args.notNull(Args.java:54)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:81)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
    at com.si.data.client.ProxyClient1.start(ProxyClient1.java:72)
    at com.si.data.client.IpcClient.main(IpcClient.java:136)

这是第二个出现错误的请求:

    private void createCurrencyPairsRequest(String currencyPairs) throws IOException {
    if (this.sessionId == null) {
        this.refreshSessioId();
    }
    String params = new StringBuilder()
            .append("?id=").append(this.sessionId)
            .append("&c=").append(currencyPairs)//The Pairs is an array
            .append("&f=").append(this.format)
            .toString();
    this.requestCurrencies = new HttpGet(this.target + params);
}

我在currencyPairsRequest()中是否做错了什么?谢谢你花时间。。

共有1个答案

白信鸿
2023-03-14

我通过调试currencyPairsRequest()解决了这个问题。。我就是这样解决的。。。我将currencyPairsRequest()返回类型从void()更改为HttpGet。

   private HttpGet createCurrencyPairsRequest(String currencyPairs) throws IOException {
       if (this.sessionId == null) {
           this.refreshSessioId();
       }
       String params = new StringBuilder()
               .append("?id=").append(this.sessionId)
               .append("&c=").append(currencyPairs)
               .append("&f=").append(this.format)
               .toString();
       this.requestCurrencies = new HttpGet(this.target + params);
    return requestCurrencies;
   }

然后,我使用以下命令调用了“currencyPairsRequest():

System.out.println(requestCurrencies);

在启动任何请求之前,从main()。。。并且,它打印了一个空的请求,这意味着我从昨天开始就一直在发送一个空的请求。。因此,我改变了我的请求,没有使用requestCurrences,而是调用函数本身并返回其值。。。

谢谢大家。。

 类似资料:
  • 我有一个WPF应用程序,需要从远程API检索配置数据。该API已经过测试,并且工作正常,返回我认为是正确的格式。我已经为响应等创建了一个数据类型,但是当我到达通过HttpClient获取响应的行时,它不会向API发送任何请求。 数据类型:- 我已经在API上运行了日志,我们没有看到请求,如果我们使用相同的代码模型调用另一个API控制器来调用Uri2(https://example.com/API/

  • 我用的是symfony4.4。7并使用HTTPClient(Symfony\Component\HTTPClient\HTTPClient)发出http请求,但探查器中未显示请求。如何使此探查器选项卡正常工作?

  • 本文向大家介绍在React.js中发出HTTP请求,包括了在React.js中发出HTTP请求的使用技巧和注意事项,需要的朋友参考一下 在典型的Web应用程序中,客户端通过浏览器发出http请求,而服务器在响应中发送带有数据的html页面。 但是在单页应用程序(SPA)中,我们只有一页,每当客户端向服务器发出http请求时,它通常都会以json / xml格式的数据进行响应。 为了发出http请求

  • 问题内容: 我需要将POST请求发送到包含gzip压缩请求参数的Web服务器。我使用的是Apache HttpClient,我已经读到它支持开箱即用的Gzip,但是我找不到任何有关如何做我需要的示例。如果有人可以发布一些示例,我将不胜感激。 问题答案: 您需要先将其转换为gzip 或(临时)。我们假设它不是一个很大的值,所以对于可用的JVM内存足够安全: 然后,可以使用HttpClient将其作为

  • 我正试图通过新的Java11HttpClient取消http请求。 这是我的测试代码: 我预计,该请求任务将在 行调用后立即被取消。因此,控制台中最后打印的行应该是 但是,当我运行这段代码时,我看到类似这样的内容: 这意味着,在我取消请求后,请求任务仍在运行…所以,这是取消请求的正确方法吗? UPD 取消请求的正确方法是(正如daniel所建议的,UPD2:在方法调用中避免NPE):