Java调用URL错误,报505
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java调用URL错误,报505相关的知识,希望对你有一定的参考价值。
我在代码中调用这个地址就会报错:
h t t p : //localhost:8080/wap/invest.action?action=investShowCoupon&xmlResponse=["activityId":1,"endDate":"2015 07 01 ","state":"02","timeLimit":"5 ","userId":1000589,"voucherId":2449,"vouchersAmount":80,"activityId":1,"endDate":"2015 07 01 ","state":"02","timeLimit":"5 ","userId":1000589,"voucherId":2450,"vouchersAmount":20,"activityId":1,"endDate":"2015 07 01 ","state":"02","timeLimit":"5 ","userId":1000589,"voucherId":2451,"vouchersAmount":100]&loanId=783&amount=700&maxTender=148&mobile=&referrerCode=
总是报java.io.IOException: Server returned HTTP response code: 505 for URL
高手帮忙看下是怎么回事(HTTP那里是因为不让我发提问,我隔开了空格)?
HttpURLConnection 请求中 参数中如果有 空格,请求则会 505错误
解决: 需要对有空格的参数 做URL编码处理。
import java.io.BufferedReader;import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import sun.net.www.protocol.http.HttpURLConnection;
import com.alibaba.fastjson.JSONObject;
public class SingleTableRestClient
private static final String targetURL = "http://localhost:8080/agd-restful/services/restful/QueryService/queryData/*?queryParam=";
public static void main(String[] args)
JSONObject obj = new JSONObject();
obj.put("XM", "匡匡");
obj.put("BIRTHDAY", <span style="color:#FF6666;">getURLEncoder</span>("1988-01-01 00:00:00,1988-12-30 00:00:00"));
String urls = targetURL + obj.toString();
requestRestServer(urls);
public static JSONObject requestRestServer(String url)
JSONObject obj = new JSONObject();
try
URL restServiceURL = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Accept", "application/json");
httpConnection.setRequestProperty("Accept-Charset", "UTF-8");
httpConnection.setRequestProperty("contentType", "UTF-8");
if (httpConnection.getResponseCode() != 200)
throw new RuntimeException("HTTP GET Request Failed with Error code : "
+ httpConnection.getResponseCode());
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream()),"utf-8"));
String output = "";
String result = "";
System.out.println("Output from Server: \\n");
while ((output = responseBuffer.readLine()) != null)
//System.out.println(output);
result = output;
obj = JSONObject.parseObject(result);
System.out.println(obj.toString());
httpConnection.disconnect();
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
return obj;
@SuppressWarnings("deprecation")
<span style="color:#FF6666;">private static String getURLEncoder(String dest)
return URLEncoder.encode(dest);
</span>
修改后 正常ok
参考技术A http://baike.baidu.com/link?url=aZn8yOd8596-PrgQ-I0J8BKxjmuCcYLi-w2qDof66XMLJDWQU3sERHdlarh2z0x1bSmNEvMzSD1_Fp6KjTrrXq看到底本回答被提问者采纳 参考技术B 1.启用了 HTTP 1.1,则可能出现此类错误。要禁用 HTTP 1.1,请单击“工具”按钮,单击“Internet 选项”,然后单击“高级”选项卡。在“设置”下,滚动到“HTTP 1.1 设置”部分,清除“使用 HTTP 1.1”复选框。如果第三方产品干扰 Internet Explorer,则可能出现此类错误。请尝试关闭所有程序,然后重新尝试访问该网页。
2.大多数浏览器都默认Web服务器支持 1.x 的版本的 HTTP 协议。但实际上如 0.9 版今天已很少使用,关键是因为和新版本的协议比起来它们的安全性和性能都差得多。 所以,如果您在浏览器中看到这个错误,可以升级您的Web服务器软件。如果版本 1.x 的请求失败,很可能是因为您的 Web 服务器不是很好地支持 1.x 协议版本, 而不是根本不支持。
java.io.IOException: Server returned HTTP response code: 411 for URL
今日调用一post方式提交的http接口,此接口在测试环境ip调用时无问题,但在生产环境通过域名调用时一直报如下错误:
java.io.IOException: Server returned HTTP response code: 411 for URL
百度之后得到:在调用时,添加如下两行代码即可,今行文以记之:
/*解决411*/ httpConnection.setRequestProperty("Content-Length","0"); DataOutputStream os = new DataOutputStream( httpConnection.getOutputStream() ); /*解决411*/
附解决方案链接:http://blog.csdn.net/pfyuit/article/details/8137777
以上是关于Java调用URL错误,报505的主要内容,如果未能解决你的问题,请参考以下文章
OBIEE 12c 中的错误通过代理调用 java 方法以将报告发送到另一台服务器
javascript onclick调用函数,进入不了函数,报函数未定义的错误