【问题标题】:Making a HttpClient request in java在java中发出HttpClient请求
【发布时间】:2015-09-15 11:28:41
【问题描述】:

我正在创建一个数据下载器,自从使用 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();
            }
        }

这会打印一行Thisdude:coole3r,但是当我发出另一个打印长行的请求时,它总是会带来这个错误:

    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() 中做错了吗? 感谢您抽出宝贵的时间..

【问题讨论】:

    标签: java arrays csv apache-httpclient-4.x


    【解决方案1】:

    我通过调试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()... 而且,它打印了null,这意味着我从昨天开始就一直在发送null 请求.. 所以,我改变了我的请求,不使用requestCurrencies,而是调用函数本身并返回它的值......

    谢谢大家..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多