Java中HTTPRequest获取数据
我想用Java做一个HTTPRequest,然后从服务器获取数据(这不是网页,数据来自数据库)。
我尝试了这个,但 getData 不起作用。
你知道我如何获取数据吗?
public static void main(String args[]) throws Exception {
URL url = new URL("http://ip-ad.com");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
System.out.println("Request method is " + httpCon.getData());
}
谢谢
I would like to do an HTTPRequest in Java and then get the data from the server (it's not a webpage the data come from a database).
I try this but the getData doesn't work.
Do you know how I can get the Data?
public static void main(String args[]) throws Exception {
URL url = new URL("http://ip-ad.com");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
System.out.println("Request method is " + httpCon.getData());
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 Web 请求的响应正文作为 获取
InputStream
with:从这里开始,这取决于响应数据的格式。如果它是 XML,则将其传递给库来解析 XML。如果您想将其读入
String
,请参阅:将网站内容读入字符串。这是将其写入本地文件的示例:You can get the response body of the web request as an
InputStream
with:From there it depends on what the format of the response data is. If it's XML then pass it to a library to parse XML. If you want to read it into a
String
see: Reading website's contents into string. Here's an example of writing it to a local file:您可以使用 http://jersey.java.net/ 。
这是一个满足您需求的简单库。
You can use http://jersey.java.net/ .
It's a simple lib for your needs.