HttpURLConnection的一个坑

引言

    最近在使用HttpURLConnection请求到另外一个应用去获取数据,刚开始用下面的url请求数据:

String url = "http://localhost:8888/example/queryData.do?query=" +  
"{\"start\":0,\"tableid\":\"EVTS\",\"rows\":10,\"token\":\"76ecc4c399e56792c5fd0905f8ffaa86\","
                + "\"order\":{},\"query\":[]}";

HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(false);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Host", "www.awebsite.com");
conn.setRequestProperty("User-Agent", "Mozilla/4.0");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Keep-Alive", "115");
conn.setRequestProperty("Connection", "keep-alive");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder data = new StringBuilder();
String s = "";
while((s = br.readLine()) != null)
    data.append(s);
String pageData = data.toString();

    上面的请求很正常,能拿到预计的数据,但是后续url需要增加一个日期时间的入参后,出问题了,修改后的url变成了下面的这样:

String url = "http://localhost:8888/example/queryData.do?query=" +  
"{\"start\":0,\"tableid\":\"EVTS\",\"rows\":10,\"token\":\"76ecc4c399e56792c5fd0905f8ffaa86\","
                + "\"order\":{},\"query\":[{"startTime":"2018-03-27 14:00:00","endTime":"2018-03-27 14:30:00"]}";

    HttpURLConnection就会报下面的这种错误:

java.io.IOException: <h1>Bad Message 400</h1><pre>reason: Unknown Version</pre>

    一直排查都没有发现前后有什么问题。

解决

    只能上网找解决办法, stackoverflow上也有人遇到这问题:

https://stackoverflow.com/questions/25088269/structr-badmessage-400-unknown-version-for-httpchanneloverhttp

113549_Vl4d_3470849.png

    解决的办法就是:把空格用转译符号代替。

总结

    这还真是一个坑,比较隐蔽。

转载于:https://my.oschina.net/u/3470849/blog/1791103

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpUrlConnectionJava 标准库中用于发送 HTTP 请求和处理 HTTP 响应的类。它提供了一种简单的方式来与远程服务器进行通信。您可以使用 HttpUrlConnection 类来建立连接、发送请求、读取响应和处理错误。 以下是使用 HttpUrlConnection 发送 GET 请求的示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpUrlConnectionExample { public static void main(String[] args) { try { URL url = new URL("http://example.com/api"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); System.out.println("Response: " + response.toString()); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个示例中,我们创建了一个 URL 对象来指定要发送请求的目标 URL。然后,我们打开一个 HttpURLConnection 连接并设置请求方法为 GET。发送请求后,我们可以获取响应码、读取响应内容,并在最后关闭连接。 您可以根据需要设置请求头、添加请求参数等。同时,HttpUrlConnection 也支持其他的 HTTP 方法,如 POST、PUT、DELETE 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值