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<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 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<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();
}
}
}