Java 从服务器获得505响应
嗨,在我的android项目中,我调用了一个Web服务,并通过查询字符串参数发送get参数,现在的问题是,若查询字符串参数值包含任何空格,那个么我将得到505个错误Java 从服务器获得505响应,java,android,apache,http-status-code-505,Java,Android,Apache,Http Status Code 505,嗨,在我的android项目中,我调用了一个Web服务,并通过查询字符串参数发送get参数,现在的问题是,若查询字符串参数值包含任何空格,那个么我将得到505个错误 URL url = new URL(urlstring.trim()); urlConnection = (HttpURLConnection) url.openConnection();
URL url = new URL(urlstring.trim());
urlConnection = (HttpURLConnection) url.openConnection();
int response = urlConnection.getResponseCode();
如果我使用
URLEncode(urlstring.trim(),“UTF-8”)
我是否也需要更改我的Web服务代码?您应该只编码参数的值:
String urlString = "http://test.com?param1=" + URLEncoder.encode(value1, "UTF-8") + "¶m2=" + URLEncoder.encode(value2, "UTF-8") ;
URL url = new URL(urlstring.trim());
urlConnection = (HttpURLConnection) url.openConnection();
int response = urlConnection.getResponseCode();
您应该只编码参数的值,而不是完整的url谢谢fiddler的快速响应,我正在编码完整的url。我在Glassfish 5上的rest服务中犯了同样的错误。我对整个url进行了编码,得到了错误的url异常,我们必须只对参数的值进行编码。