package com.httpclicent;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TesthttpClient {
/**
* 使用httpPost http登陆接口(无http摘要认证)
*/
public static void testHttpClient(){
//1.创建httpClient对象 (使用https协议,需要略过ssl验证,这里使用MySSLSocketFactory)
HttpClient httpclient = new DefaultHttpClient();
//url https地址
String url ="http://192.168.10.215:8080/tnserver/property_manager/login";
try {
//2.创建列参数(httpPost使用)
HttpPost httppost = new HttpPost(url);
List<NameValuePair> list = new ArrayList<NameValuePair>();
//将用户名与密码作为参数写入(username,password这两个参数是 根据你的接口协议而定)
list.add(new BasicNameValuePair("username","admin"));
list.add(new BasicNameValuePair("password","123456"));
//3.写入请求头
httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
//4.将参数写入到httpost中,并设置编码格式为UTF-8
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8");
httppost.setEntity(entity);
//5.执行
HttpResponse post= httpclient.execute(httppost);
//6.执行完毕,判断返回code码,200为正常
if (post.getStatusLine().getStatusCode() == 200) {
//7.获取返回json格式数据 注意,这里不能直接转java.lang.String进行强转,
String conResult = EntityUtils.toString(post.getEntity());
//8.处理返回数据....(完)
JSONObject sobj = new JSONObject();
sobj = JSONObject.fromObject(conResult);
System.out.println(sobj);
} else {
System.out.println("失败....");
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 使用httpPost 参数为JSON格式时(无http摘要认证)
*/
public static void testHttpClient1(){
HttpClient httpclient = new DefaultHttpClient();
String url ="http://192.168.10.215:8080/tnserver/property_manager/login";
try {
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
//此处略有不同的是,它是直接将JSON格式数据写如到httppost.setEntity实体中
//并采用了StringEntity工具类,来进行转换。。。。
JSONObject json = new JSONObject();
json.put("username", "admin@qq.com");
json.put("password", "123456");
StringEntity entity = new StringEntity(json.toString(),"UTF-8");
httppost.setEntity(entity);
HttpResponse post= httpclient.execute(httppost);
if (post.getStatusLine().getStatusCode() == 200) {
String conResult = EntityUtils.toString(post.getEntity());
JSONObject sobj = new JSONObject();
sobj = JSONObject.fromObject(conResult);
System.out.println(sobj);
} else {
System.out.println("失败....");
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
/**
* http 有摘要认证时
*/
public static void testHttpClient2(){
HttpClient httpclient = new DefaultHttpClient();
String url ="http://192.168.10.216:8080/tnserver/property_manager/login";
try {
HttpPost httppost = new HttpPost(url);
//==========https认证过程=====================如无需采用https协议进行调用,可忽略https认证过程
//注:此方法只用于import org.apache.http.client.HttpClient; 新版本的认证
//旧版本:import org.apache.commons.httpclient 认证方法如下:
//Credentials defaultcreds = new UsernamePasswordCredentials("userxxx","pwdxxx");
//httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds);
//注意。 旧版本的认证方式导入的包是 带 org.apache.commons.xxxx
//创建https认证对象
CredentialsProvider credsProvider = new BasicCredentialsProvider();
//写入认证的用户名与密码
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "test123");
//创建验证
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
creds);
//将认证写入到httpClient中
((DefaultHttpClient)httpclient).setCredentialsProvider(credsProvider);
//=========Https认证===end=============================
httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
JSONObject json = new JSONObject();
json.put("username", "admin@qq.com");
json.put("password", "123456");
StringEntity entity = new StringEntity(json.toString());
httppost.setEntity(entity);
HttpResponse post= httpclient.execute(httppost);
if (post.getStatusLine().getStatusCode() == 200) {
String conResult = EntityUtils.toString(post.getEntity());
JSONObject sobj = new JSONObject();
sobj = JSONObject.fromObject(conResult);
System.out.println(sobj);
} else {
System.out.println("失败....");
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
/**
* http中存在认证,并采用HTTPS方式调用
*/
public static void testHttpClient3(){
//1.创建httpClient对象 (使用https协议,需要略过ssl验证,这里使用MySSLSocketFactory)
HttpClient httpclient = MySSLSocketFactory.getNewHttpClient();
String url ="https://192.168.10.216:443/tnserver/property_manager/login";
try {
HttpPost httppost = new HttpPost(url);
//==========https认证过程===================== 如无需采用https协议进行调用,可忽略https认证过程
//注:此方法只用于import org.apache.http.client.HttpClient; 新版本的认证
//旧版本:import org.apache.commons.httpclient 认证方法如下:
//Credentials defaultcreds = new UsernamePasswordCredentials("userxxx","pwdxxx");
//httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds);
//注意。 旧版本的认证方式导入的包是 带 org.apache.commons.xxxx
//创建https认证对象
CredentialsProvider credsProvider = new BasicCredentialsProvider();
//写入认证的用户名与密码
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "test123");
//创建验证
credsProvider.setCredentials(
new AuthScope(Au
没有合适的资源?快使用搜索试试~ 我知道了~
httpClient实例httpClient调用 http/https实例 忽略SSL验证
共43个文件
class:17个
jar:12个
xml:2个
5星 · 超过95%的资源 需积分: 50 359 下载量 52 浏览量
2016-03-30
17:19:44
上传
评论 2
收藏 2.1MB ZIP 举报
温馨提示
以上自己测试是可行的,直接拿过去导入即可,直接用main
资源推荐
资源详情
资源评论
收起资源包目录
test.zip (43个子文件)
test
src
com
httpclicent
MySSLSocketFactory.java 4KB
TesthttpClient.java 9KB
.classpath 2KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 414B
org.eclipse.core.resources.prefs 57B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 471B
.jsdtscope 522B
org.eclipse.jdt.core.prefs 364B
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
index.jsp 12KB
WEB-INF
classes
com
testapi
apitest.class 5KB
httpclicent
MySSLSocketFactory$1.class 1KB
MySSLSocketFactory.class 4KB
TesthttpClient.class 5KB
test
InstallCert.class 7KB
Demo.class 690B
Demo$2.class 1014B
InstallCert$SavingTrustManager.class 1KB
Y.class 1KB
Test$1.class 979B
HttpURLConnectionExample.class 4KB
HttpURLConnectionExample$1.class 1KB
Bs.class 640B
Demo$1.class 1KB
As.class 679B
HttpsTest.class 2KB
Test.class 2KB
lib
httpclient-cache-4.1.3.jar 105KB
commons-logging-1.1.1.jar 59KB
httpcore-4.1.4.jar 177KB
commons-lang-2.4.jar 256KB
ezmorph-1.0.4.jar 84KB
httpmime-4.1.3.jar 26KB
json-lib-2.4-jdk15.jar 155KB
commons-codec.jar 46KB
commons-beanutils-1.8.0.jar 226KB
commons-lang3-3.1.jar 308KB
httpclient-4.1.3.jar 344KB
commons-collections-3.2.jar 558KB
web.xml 647B
共 43 条
- 1
鱼遇上了猫
- 粉丝: 58
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页