package hasau;
import java.io.BufferedReader;
import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.params.ConnRouteParams; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; /** * * @author hasau * 2010-11-5 上午09:51:25 */ public class httpclientStudy {
public static void main(String[] args) throws Exception { // javaHttpClient1(); httpClientCookies(); } private static void httpClientCookies() throws Exception{ HttpClient httpClient = new DefaultHttpClient(); HttpContext httpContext = new BasicHttpContext(); HttpGet get = new HttpGet("http://localhost:8080/sgcms/back/logout.do"); HttpResponse httpResponse = httpClient.execute(get, httpContext); HttpEntity entity = httpResponse.getEntity(); String charSet = null; if(entity!=null){ charSet = EntityUtils.getContentCharSet(entity); System.out.println(charSet); InputStream is = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8")); String text = null; String line = br.readLine(); StringBuffer sb = new StringBuffer(); while(line != null){ sb.append(line+"\n"); line = br.readLine(); } is.close(); } HttpPost httpPost = new HttpPost("http://localhost:8080/sgcms/back/ajaxJsonLogin.do'"); List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("admin.loginName","hasau")); list.add(new BasicNameValuePair("admin.passwordMd5","hasau")); list.add(new BasicNameValuePair("admin.authCode","")); httpPost.setEntity(new UrlEncodedFormEntity(list,charSet)); httpResponse = httpClient.execute(httpPost); entity = httpResponse.getEntity(); if(entity != null){ InputStream is = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8")); String line = br.readLine(); String text = null; StringBuffer sb = new StringBuffer(); while(line != null){ sb.append(line+"\n"); line=br.readLine(); } System.out.println(sb.toString()); } httpClient.getConnectionManager().shutdown(); } private static void javaHttpClient1() throws ClientProtocolException, IOException{
HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, new HttpHost("121.12.249.207",3128)); HttpGet get =new HttpGet("http://www.baidu.com"); HttpContext context = new BasicHttpContext(); HttpResponse response = httpClient.execute(get,context); HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);//http://localhost:8080 HttpUriRequest request = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);// cms/back/hasau.jsp HttpEntity entity = response.getEntity(); String charset = EntityUtils.getContentCharSet(entity); System.out.println(charset); if(entity!=null){ InputStream is = entity.getContent(); BufferedReader buf = new BufferedReader(new InputStreamReader(is,"gbk")); String content=""; String line = buf.readLine(); while(line!=null){ content += line+"\n"; line=buf.readLine(); } System.out.println(content); is.close(); } httpClient.getConnectionManager().shutdown(); } private static void javaNetJsoup() throws IOException {
String key = "http://www.baidu.com"; URL url = new URL(key); Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("192.168.12.32",80)); HttpURLConnection con = (HttpURLConnection)url.openConnection(proxy); con.setFollowRedirects(false); con.setRequestMethod("GET"); BufferedReader buf = new BufferedReader(new InputStreamReader(con.getInputStream(),"gbk")); InputStream in = url.openStream(); buf = new BufferedReader(new InputStreamReader(in,"gbk")); String line = buf.readLine(); String content = ""; while(line!=null){ content +=line; line=buf.readLine(); } in.close(); System.out.println(content); Document document = Jsoup.parse(url, 1000*10); System.out.println(document.html()); } } |
|
来自: 闪宁斯达 > 《httpclient》