在java中发出HttpClient请求
我正在创建一个数据下载程序,自从使用httpClient以来,它一直给我带来问题 此代码将所需的所有变量附加到URL:在java中发出HttpClient请求,java,arrays,csv,apache-httpclient-4.x,Java,Arrays,Csv,Apache Httpclient 4.x,我正在创建一个数据下载程序,自从使用httpClient以来,它一直给我带来问题 此代码将所需的所有变量附加到URL: private void createOpenSession() { String params = new StringBuilder() .append("?u=").append(this.user) .append("&p=").append(this.password) .appen
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
,它都会以一种简单的方式打印出标题(这正是我需要它做的)
这只打印一行Thisdude:Cool3r
,但当我发出另一个打印长行的请求时,它总是会带来以下错误:
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()
中做错了什么?
感谢您花费的时间。我通过调试
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():
在启动任何请求之前,从main()。。。
而且,它打印了null
,这意味着我从昨天起就一直在发送null
请求。。
因此,我没有使用requestCurrences
,而是通过调用函数本身并返回其值来更改请求
谢谢大家。我通过调试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():
在启动任何请求之前,从main()。。。
而且,它打印了null
,这意味着我从昨天起就一直在发送null
请求。。
因此,我没有使用requestCurrences
,而是通过调用函数本身并返回其值来更改请求
谢谢大家
System.out.println(requestCurrencies);