JAVA HTTPCLIENT

来源:互联网 发布:数据库设计与实现 编辑:程序博客网 时间:2024/06/05 00:11

来自 http://stackoverflow.com/questions/1485708/how-do-i-do-a-http-get-in-java

1、

GetMethod get = new GetMethod("http://httpcomponents.apache.org");// execute method and handle any error responses....InputStream in = get.getResponseBodyAsStream();// Process the data from the input stream.get.releaseConnection();

2、

import java.io.*;import java.net.*;public class c {   public String getHTML(String urlToRead) {      URL url;      HttpURLConnection conn;      BufferedReader rd;      String line;      String result = "";      try {         url = new URL(urlToRead);         conn = (HttpURLConnection) url.openConnection();         conn.setRequestMethod("GET");         rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));         while ((line = rd.readLine()) != null) {            result += line;         }         rd.close();      } catch (Exception e) {         e.printStackTrace();      }      return result;   }   public static void main(String args[])   {     c c = new c();     System.out.println(c.getHTML(args[0]));   }}



3、

urlString = "http://wherever.com/someAction?param1=value1¶m2=value2....";URL url = new URL(urlString);URLConnection conn = url.openConnection();InputStream is = conn.getInputStream()