java http请求验证_java中httpclient实现digest验证的请求

1、首先介绍如何使用HttpClient发起GET和POST请求 GET 方式://先将参数放入List,再对参数进行URL编码ListBasicNameValuePair params = new LinkedListBasicNameValuePair();params.add(new BasicNameValuePair(param1, 中国));params.add(new BasicNameVal

1、首先介绍如何使用HttpClient发起GET和POST请求GET 方式:

//先将参数放入List,再对参数进行URL编码

List params = new LinkedList();

params.add(new BasicNameValuePair("param1", "中国"));

params.add(new BasicNameValuePair("param2", "value2"));

//对参数编码

String param = URLEncodedUtils.format(params, "UTF-8");

//baseUrl

String baseUrl = "

//将URL与参数拼接

HttpGet getMethod = new HttpGet(baseUrl + "?" + param);

HttpClient httpClient = new DefaultHttpClient();

try {

HttpResponse response = httpClient.execute(getMet搜索hod); //发起GET请求

Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码

Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

POST方式:

//和GET方式一样,先将参数放入List

params = new LinkedList();

params.add(new BasicNameValuePair("param1", "Post方法"));

params.add(new BasicNameValuePair("param2", "第二个参数"));

try {

HttpPost postMethod = new HttpPost(baseUrl);

postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中

HttpResponse response = httpClient.execute(postMethod); //执行POST方法

Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码

Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

2、httpclient的digest验证,需要tomcat服务器配置digest验证,具体配置参见先前博文。

[java]

view plaincopyprint?

packagetest.util;

importjava.net.URI;

importjava.util.ArrayList;

importjava.util.List;

importorg.apache.commons.httpclient.auth.AuthPolicy;

importorg.apache.http.HttpResponse;

importorg.apache.http.auth.AuthScope;

importorg.apache.http.auth.Credentials;

importorg.apache.http.auth.UsernamePasswordCredentials;

importorg.apache.http.client.methods.HttpUriRequest;

importorg.apache.http.impl.auth.DigestSchemeFactory;

importorg.apache.http.impl.client.DefaultHttpClient;

/**

* @author Administrator

*

*/

publicclassHTTPDigestClient {

privateURI serverURI =null;

String response = null;

privateDefaultHttpClient httpClient =newDefaultHttpClient();

/**

* constructor

*/

publicHTTPDigestClient(String userName, String passWord, String url) {

try{

serverURI = newURI(url);

Credentials creds = newUsernamePasswordCredentials(userName,

passWord);

httpClient.getCredentialsProvider().setCredentials(

newAuthScope(serverURI.getHost(), serverURI.getPort), (Credentials) creds);

httpClient.getParams().setParameter(

AuthPolicy.AUTH_SCHEME_PRIORITY, Collections.singleton(AuthPolicy.DIGEST));

httpClient.getAuthSchemes().register(AuthPolicy.DIGEST,

newDigestSchemeFactory());

} catch(Exception e) {

}

}

/**

* send request to server

*

* @param httpClient

* @param httpUriRequest

* @return response HttpResponse

*/

publicHttpResponse send(HttpUriRequest httpUriRequest) {

HttpResponse response = null;

try{

if(null== httpClient) {

returnresponse;

}

response = httpClient.execute(httpUriRequest);

} catch(Exception e) {

}

returnresponse;

}

}

package test.util;

import java.net.URI;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.httpclient.auth.AuthPolicy;

import org.apache.http.HttpResponse;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.Credentials;

import org.apache.http.auth.UsernamePasswordCredentials;

import org.apache.http.client.methods.HttpUriRequest;

import org.apache.http.impl.auth.DigestSchemeFactory;

import org.apache.http.impl.client.DefaultHttpClient;

/**

* @author Administrator

*

*/

public class HTTPDigestClient {

private URI serverURI = null;

String response = null;

private DefaultHttpClient httpClient = new DefaultHttpClient();

/**

* constructor

*/

public HTTPDigestClient(String userName, String passWord, String url) {

try {

serverURI = new URI(url);

Credentials creds = new UsernamePasswordCredentials(userName,

passWord);

httpClient.getCredentialsProvider().setCredentials(

new AuthScope(serverURI.getHost(), serverURI.getPort), (Credentials) creds);

httpClient.getParams().setParameter(

AuthPolicy.AUTH_SCHEME_PRIORITY, Collections.singleton(AuthPolicy.DIGEST));

httpClient.getAuthSchemes().register(AuthPolicy.DIGEST,

new DigestSchemeFactory());

} catch (Exception e) {

}

}

/**

* send request to server

*

* @param httpClient

* @param httpUriRequest

* @return response HttpResponse

*/

public HttpResponse send(HttpUriRequest httpUriRequest) {

HttpResponse response = null;

try {

if (null == httpClient) {

return response;

}

response = httpClient.execute(httpUriRequest);

} catch (Exception e) {

}

return response;

}

}

服务器端必须加入域

设置realm 为空或者当前域名 测试成功

用户名 密码用的是域账户

下面为basic验证方式 ,测试通过

[java]

view plaincopyprint?

packagetest.util;

importjava.net.URI;

importjava.util.Collections;

importorg.apache.commons.httpclient.auth.AuthPolicy;

importorg.apache.http.HttpResponse;

importorg.apache.http.auth.AuthScope;

importorg.apache.http.auth.Credentials;

importorg.apache.http.auth.UsernamePasswordCredentials;

importorg.apache.http.client.CredentialsProvider;

importorg.apache.http.client.methods.HttpUriRequest;

importorg.apache.http.impl.auth.BasicSchemeFactory;

importorg.apache.http.impl.client.DefaultHttpClient;

/**

* @author Administrator

*

*/

publicclassHTTPBasicClient {

privateURI serverURI =null;

String response = null;

privateDefaultHttpClient httpClient =newDefaultHttpClient();

/**

* constructor

*

*/

publicHTTPBasicClient(String userName, String passWord, String url) {

try{

serverURI = newURI(url);

UsernamePasswordCredentials creds = newUsernamePasswordCredentials(

userName, passWord);

CredentialsProvider credsProvider = httpClient

.getCredentialsProvider();

credsProvider.setCredentials(newAuthScope(serverURI.getHost(),

serverURI.getPort()), (Credentials) creds);

httpClient.getParams().setParameter(

AuthPolicy.AUTH_SCHEME_PRIORITY,

Collections.singleton(AuthPolicy.BASIC));

httpClient.getAuthSchemes().register(AuthPolicy.BASIC,

newBasicSchemeFactory());

} catch(Exception e) {

}

}

/**

* send request to server

*

* @param httpClient

* @param httpUriRequest

* @return response HttpResponse

*/

publicHttpResponse send(HttpUriRequest httpUriRequest) {

HttpResponse response = null;

try{

if(null== httpClient) {

returnresponse;

}

response = httpClient.execute(httpUriRequest);

} catch(Exception e) {

}

returnresponse;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值