HttpClient例子一个返回html一个返回字符流

来源:互联网 发布:帝国cms内容页js调用 编辑:程序博客网 时间:2024/05/21 05:07
import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.http.Header;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;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.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;public class TestHttpClient {private CloseableHttpClient httpclient;private HttpPost httppost;// 用于提交登陆数据private HttpGet httpget;// 用于获得登录后的页面private String login_success;// 用于构造上面的HttpGetpublic TestHttpClient() {httpclient = HttpClients.createDefault();// 人人的登陆界面网址httppost = new HttpPost("http://www.renren.com/PLogin.do");}public void logIn(String name, String password) throws Exception {// 打包将要传入的参数List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("email", name));params.add(new BasicNameValuePair("password", password));httppost.setEntity(new UrlEncodedFormEntity(params));try {// 提交登录数据HttpResponse re = httpclient.execute(httppost);// 获得跳转的网址Header locationHeader = re.getFirstHeader("Location");// 登陆不成功if (locationHeader == null) {System.out.println("登陆不成功,请稍后再试!");return;} else// 成功{login_success = locationHeader.getValue();// 获取登陆成功之后跳转链接System.out.println("成功之后跳转到的网页网址:" + login_success);}} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void PrintText() throws IOException {httpget = new HttpGet(login_success);HttpResponse re2 = null;try {re2 = httpclient.execute(httpget);// 输出登录成功后的页面String str = EntityUtils.toString(re2.getEntity());System.out.println(str);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {httppost.abort();httpget.abort();httpclient.close();}}public static void main(String[] args) throws Exception {String name = "xxx@163.com", password = "xxx";// 自己的账号,口令TestHttpClient lr = new TestHttpClient();lr.logIn(name, password);lr.PrintText();}}







import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;public class Testa2 {public static void main(String[] args) {        //创建HttpClientBuilder        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();        //HttpClient        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder");        //httpPost.setConfig(DEFAULT);        // 创建参数队列        List<NameValuePair> formparams = new ArrayList<NameValuePair>();        //formparams.add(new BasicNameValuePair("email", "xxx@163.com"));       // formparams.add(new BasicNameValuePair("password", "xxx"));        UrlEncodedFormEntity entity;        try {            entity = new UrlEncodedFormEntity(formparams, "UTF-8");            httpPost.setEntity(entity);            HttpResponse httpResponse;            //post请求            httpResponse = closeableHttpClient.execute(httpPost);            //getEntity()            HttpEntity httpEntity = httpResponse.getEntity();            if (httpEntity != null) {                //打印响应内容                System.out.println("response:" + EntityUtils.toString(httpEntity, "UTF-8"));            }            //释放资源            closeableHttpClient.close();        } catch (Exception e) {            e.printStackTrace();        }}}


0 0
原创粉丝点击