html可以用字符流么,HttpClient例子一个返回html一个返回字符流

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;// 用于构造上面的HttpGet

public TestHttpClient() {

httpclient = HttpClients.createDefault();

// 人人的登陆界面网址

httppost = new HttpPost("http://www.renren.com/PLogin.do");

}

public void logIn(String name, String password) throws Exception {

// 打包将要传入的参数

List params = new ArrayList();

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 block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.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 block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.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 formparams = new ArrayList();

//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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值