HttpClient异常:java.lang.IllegalArgumentException:使用GetMethod()发送请求时,host参数为null

时间:2016-12-02 14:07:44

标签: java web-services apache-commons-httpclient

首先,我的问题与其他人不重复。我在过去2天内查询了我的问题,但没有找到任何解决方案。我找到的解决方案是针对PostMethod,但问题是GetMethod。 由于一些机密数据问题,我无法共享确切的endpointUrl,但我给出了一个虚拟endpointUrl。 下面的代码工作正常并返回一个csv文件,直到endPointUrlhttps://abcxyz.com/incident.do?CSV(abcxyz是这里唯一的虚拟部分),:

HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
Credentials creds = new UsernamePasswordCredentials(userName, password);
client.getState().setCredentials(AuthScope.ANY, creds);

GetMethod method=new GetMethod(endPointUrl);
int status = client.executeMethod(method); 

然后我需要更改成为https://abcxyz.com/incident.do?CSV&sys_param_query=active=true^sys_updated_onBETWEENjavascript:gs.dateGenerate(%272016-11-20%27,%2700:10:00%27)@javascript:gs.dateGenerate(%272016-11-24%27,%2712:59:59%27)的endPointUrl,然后上面的代码开始给出java.lang.IllegalArgumentException: Invalid uri异常。然后我用Google搜索并发现对于更大的网址或复杂的网址,我们需要使用URLEncoder.encode(endPointUrl, UTF-8)正确编码(UTF-8)。代码变成了:

HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
Credentials creds = new UsernamePasswordCredentials(userName, password);
client.getState().setCredentials(AuthScope.ANY, creds);

GetMethod method=new GetMethod(URLEncoder.encode(endPointUrl, UTF-8));
int status = client.executeMethod(method);

现在这段代码开始抱怨java.lang.IllegalArgumentException: host parameter is null 我再次尝试解决这个问题,并提出了一些解决方案,如下所示:

HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
Credentials creds = new UsernamePasswordCredentials(userName, password);
client.getState().setCredentials(AuthScope.ANY, creds);

HostConfiguration hf=new HostConfiguration();
hf.setHost("ven01623.service-now.com", 443); 
GetMethod method=new GetMethod(URLEncoder.encode(endPointUrl, UTF-8));
method.setHostConfiguration(hf);
int status = client.executeMethod(method);

但现在,它正在抱怨org.apache.commons.httpclient.URIException: invalid port number,我不知道现在要做什么,因为它是一个端口号为443的https连接。

我在这里还有一个问题,https://abcxyz.com/incident.do?CSVGetMethod method=new GetMethod(endPointUrl);下正常运行,那为什么在GetMethod method=new GetMethod(URLEncoder.encode(endPointUrl, UTF-8));下抱怨主机参数为空? 非常需要帮助。 感谢

0 个答案:

没有答案