【问题标题】:getting 505 responce from server从服务器获得 505 响应
【发布时间】:2012-11-06 14:12:35
【问题描述】:

嗨,在我的 android 项目中,我正在调用 web 服务并通过查询字符串参数发送 get 参数,现在的问题是,如果查询字符串参数值包含任何空格,那么我会收到 505 错误

                    URL url = new URL(urlstring.trim());                        
                    urlConnection = (HttpURLConnection) url.openConnection();
                    int response = urlConnection.getResponseCode();

我有一个疑问,如果我使用URLEncode(urlstring.trim(),"UTF-8")我是否还需要更改我的网络服务代码?

【问题讨论】:

  • 你应该只编码你的参数值,而不是完整的 URL

标签: java android apache http-status-code-505


【解决方案1】:

您应该只对参数的值进行编码:

    String urlString = "http://test.com?param1=" + URLEncoder.encode(value1, "UTF-8") + "&param2=" + URLEncoder.encode(value2, "UTF-8") ;

    URL url = new URL(urlstring.trim());                        
    urlConnection = (HttpURLConnection) url.openConnection();
    int response = urlConnection.getResponseCode();

【讨论】:

  • 感谢提琴手的快速响应,我正在编码完整的网址。
  • 我在 Glassfish 5 上的 rest 服务也犯了同样的错误。我对整个 url 进行了编码,得到了 malformedUrlException,我们必须只编码参数的值。
猜你喜欢
  • 2019-09-11
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
相关资源
最近更新 更多