现在的位置: 首页 > 综合 > 正文

JAVA HTTPCLIENT

2013年11月15日 ⁄ 综合 ⁄ 共 983字 ⁄ 字号 评论关闭

来自 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() 

抱歉!评论已关闭.