Httpclient向远程服务发送请求并得到返回值

Android 专栏收录该内容
286 篇文章 0 订阅
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());
  }
}

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

doloveme

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值