Java HttpURLConnection GET 调用参数不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29941376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpURLConnection GET call with parameters not working
提问by My God
I have a very simple code which is not working.
我有一个非常简单的代码,它不起作用。
Class HttoCon:
类 HttoCon:
package test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class HttpCon {
public static void main(String[] args) {
try {
sendGet();
} catch (Exception e) {
e.printStackTrace();
}
}
// HTTP GET request
private static void sendGet() throws Exception {
String url = "http://myweb.com/public/resource/data/stream";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("begin", "1430295300000");
con.setRequestProperty("end", "1430297279988");
con.setRequestProperty("id", "140621");
con.setRequestProperty("time", "FIFTEEN_MINUTE");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
Error Trace:
错误跟踪:
{"fault":{"exceptionType":"MissingServletRequestParameterException","host":"12.205.101.123","cause":"Required Long parameter 'id' is not present","status":"400","query":"/public/resource/data/stream","stackTrace":"org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter 'id' is not present\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseMissingParameterException(AnnotationMethodHandlerAdapter.java:774)\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:509)\n\tat
NOTE:
笔记:
If I hit the url directly in browser it works fine.
如果我直接在浏览器中点击 url,它工作正常。
UPDATE:
更新:
Using curl:
使用卷曲:
curl -X GET http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE
Above curl doesn't work and the exception is same -
以上 curl 不起作用,异常是相同的 -
curl -X GET 'http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE'
This curl works fine.
这种卷曲效果很好。
采纳答案by Craig Parkinson
You create a String containing the parameters, which then gets appended onto the URL. Create a URL Obj from this. For example :-
您创建一个包含参数的字符串,然后将其附加到 URL 上。从此创建一个 URL Obj。例如 :-
String this.url = "http://myweb.com/public/resource/data/stream";
this.url += "?begin=1430295300000&end=1430297279988&id=140621
&time=FIFTEEN_MINUTE";
this.urlObj = new URL(this.url);
Then you create the connection as in your original example.
然后按照原始示例创建连接。
回答by Axel Amthor
openConnection
will establish the connection and issue the GET
. Subsequent settings of GET
parameters on the returned URLConnection object are ignored, since the URL has already been open.
openConnection
将建立连接并发出GET
. GET
返回的 URLConnection 对象上的参数的后续设置将被忽略,因为 URL 已经打开。
Add the parameters to the URL as query string params just as the link above and open that link
像上面的链接一样将参数作为查询字符串参数添加到 URL 中,然后打开该链接
or
或者
try to post the parameters to the server (which is even better)
尝试将参数发布到服务器(甚至更好)