Server returned HTTP response code: 505 问题解决




在使用URL 读取http流时,出现505异常:

Server returned HTTP response code: 505 
而将URL拷贝到浏览器地址栏中,却可以正常访问。

[java]  view plain  copy
  1. String sUrl = baseUrl + "param1=" + productName+ "&type=" + dataType;  
  2. URL url = new URL(sUrl);  
  3. InputStream is = url.openStream();   


2、分析

经检查,发现时URL的参数带有空格时,抛出的该异常,即上传参数productName的值形如:xxxxxx   xxxxxxx"。

3、解决

解决办法:

对可能含有空格字符的参数进行URL编码,使用java.net.URLEncoder类的的enchode()方法对字符串进行编码。

修改后的代码如下:

[java]  view plain  copy
  1. String pn = URLEncoder.encode(productName, "utf-8");  
  2. String sUrl = baseUrl + "param1=" + pn + "&type=" + dataType;  
  3. URL url = new URL(sUrl);  
  4. InputStream is = url.openStream();   
问题解决。


在使用URL 读取http流时,出现505异常:

Server returned HTTP response code: 505 
而将URL拷贝到浏览器地址栏中,却可以正常访问。

[java]  view plain  copy
  1. String sUrl = baseUrl + "param1=" + productName+ "&type=" + dataType;  
  2. URL url = new URL(sUrl);  
  3. InputStream is = url.openStream();   


2、分析

经检查,发现时URL的参数带有空格时,抛出的该异常,即上传参数productName的值形如:xxxxxx   xxxxxxx"。

3、解决

解决办法:

对可能含有空格字符的参数进行URL编码,使用java.net.URLEncoder类的的enchode()方法对字符串进行编码。

修改后的代码如下:

[java]  view plain  copy
  1. String pn = URLEncoder.encode(productName, "utf-8");  
  2. String sUrl = baseUrl + "param1=" + pn + "&type=" + dataType;  
  3. URL url = new URL(sUrl);  
  4. InputStream is = url.openStream();   
问题解决。

猜你喜欢

转载自blog.csdn.net/cafuf/article/details/78923236