我想用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());
}谢谢
发布于 2011-05-10 16:06:08
您可以使用以下命令以InputStream的形式获取web请求的响应正文:
httpCon.getInputStream();InputStream in = httpCon.getInputStream();
OutputStream out = new FileOutputStream("file.dat");
out = new BufferedOutputStream(out);
byte[] buf = new byte[8192];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.close();发布于 2011-05-10 16:03:09
您可以使用http://jersey.java.net/。
这是一个满足您需求的简单库。
https://stackoverflow.com/questions/5946932
复制相似问题