抱歉,我对 Java 很陌生。
我偶然发现 HttpGet 和 HttpPost 似乎非常适合我的需求,但有点啰嗦。我写了一个相当糟糕的包装类,但有谁知道在哪里可以获得更好的包装类?
理想情况下,我可以做到
String response = fetchContent("http://url/", postdata);
其中 postdata 是可选的。
谢谢!
抱歉,我对 Java 很陌生。
我偶然发现 HttpGet 和 HttpPost 似乎非常适合我的需求,但有点啰嗦。我写了一个相当糟糕的包装类,但有谁知道在哪里可以获得更好的包装类?
理想情况下,我可以做到
String response = fetchContent("http://url/", postdata);
其中 postdata 是可选的。
谢谢!
HttpClient听起来像你想要的。您当然不能在一行中完成上述操作,但它是一个完整的 HTTP 库,包含 Get/Post 请求(以及其他请求)。
我会考虑使用HttpClient库。从他们的文档中,您可以生成这样的 POST:
PostMethod post = new PostMethod("http://jakarata.apache.org/");
NameValuePair[] data = {
new NameValuePair("user", "joe"),
new NameValuePair("password", "bloggs")
};
post.setRequestBody(data);
// execute method and handle any error responses.
...
InputStream in = post.getResponseBodyAsStream();
// handle response.
如果您最终需要这些选项,则有许多用于配置客户端的高级选项。