【发布时间】:2016-01-13 10:06:24
【问题描述】:
我尝试发送 http 请求并使用 Java 代码获取结果
这是服务器端:
public class Testws {
@GET
@Path("/test/{id}")
@Produces("application/xml")
public Integer getAssets(@PathParam("id") int id){
}
}
这是客户端:
URL url = new URL("http://xxx/route/test/1");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.connect();
final int responseCode = connection.getResponseCode();
当我运行这个 java 代码时,我得到了 405 状态码,但是当我将链接 (http://xxx/route/test/1) 直接放在浏览器中时,它工作正常,我得到了 200 作为响应码。 我该如何解决这个问题?
【问题讨论】:
-
当你想要一个 GET 请求时,为什么要使用
setDoOutput(true)? -
试试
connection.sendGet(); -
谢谢大家的回复。 @Fran: 连接.sendGet();不工作@Thilo:你说得对,问题是
setDoOutput(true)我删除了它,现在它工作正常谢谢。
标签: java web-services http