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

HttpClient异常:java.lang.IllegalArgumentException:使用GetMethod()发送请求时主机参数为null,java,web-services,apache-commons-httpclient,Java,Web Services,Apache Commons Httpclient,首先,我的问题不会重复给别人。我正在查询过去两天的问题,但没有找到任何解决方案。我找到的解决方案是针对PostMethod的,但我遇到的问题是GetMethod。 由于某些机密数据问题,我无法共享确切的endpointUrl,但我提供了一个虚拟的endpointUrl。 下面的代码工作正常并返回csv文件,直到endPointUrl被https://abcxyz.com/incident.do?CSV(abcxyz是此处唯一的虚拟零件): 然后我需要更改endPointUrl,它变成了https

首先,我的问题不会重复给别人。我正在查询过去两天的问题,但没有找到任何解决方案。我找到的解决方案是针对
PostMethod
的,但我遇到的问题是
GetMethod
。 由于某些机密数据问题,我无法共享确切的endpointUrl,但我提供了一个虚拟的endpointUrl。 下面的代码工作正常并返回csv文件,直到
endPointUrl
https://abcxyz.com/incident.do?CSV
(abcxyz是此处唯一的虚拟零件):

然后我需要更改endPointUrl,它变成了
https://abcxyz.com/incident.do?CSV&sys_param_query=active=true^系统更新了javascript:gs.dateGenerate(%272016-11-20%27,%2700:10:00%27)@javascript:gs.dateGenerate(%272016-11-24%27,%2712:59:59%27)
然后上面的代码开始给出
java.lang.IllegalArgumentException:Invalid uri
异常。然后我在谷歌上搜索了一下,发现对于较大的url或复杂的url,我们需要使用
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参数为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:端口号无效
,我不知道现在该怎么办,因为它是一个https连接,端口号为443

我还有一个问题,
https://abcxyz.com/incident.do?CSV
GetMethod=newgetmethod(endPointUrl)下运行良好
那么为什么在
GetMethod=newgetmethod(urlcoder.encode(endPointUrl,UTF-8))下抱怨主机参数为null?
非常需要帮助。

感谢

尽管名称不同,URLEncoder用于编码参数名称和值,而不是整个URL。您需要使用URI类来正确编码URL。@EJP我认为URI编码不是问题,因为我打印了URI
new GetMethod(urlcoder.encode(endPointUrl,UTF-8)).getURI()并打印了https%3A%2F%2Fabcxyz.com%2F%2favent.do%3FCSV
,根据javaDoc我猜这是正确的编码
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);