`
hao861002
  • 浏览: 84742 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

HttpClient 学习整理(4)

阅读更多

5 提交XML格式参数

提交XML格式的参数很简单,仅仅是一个提交时候的ContentType问题,下面的例子演示从文件文件中读取XML信息并提交给服务器的过程,该过程可以用来测试Web服务。

import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/** 
 *用来演示提交XML格式数据的例子
*/

public class PostXMLClient {

   public
static void main(String[] args) throws Exception {
      File input =
new File(“test.xml”);
      PostMethod post =
new PostMethod(“http://localhost:8080/httpclient/xml.jsp”);

      // 设置请求的内容直接从文件中读取
      post.setRequestBody( new FileInputStream(input)); 
      if (input.length() < Integer.MAX_VALUE)
         post.setRequestContentLength(input.length());
      else
         post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);

      // 指定请求内容的类型
      post.setRequestHeader( "Content-type" , "text/xml; charset=GBK" );
      HttpClient httpclient = new HttpClient();
      int result = httpclient.executeMethod(post);
      System.out.println(
"Response status code: " + result);
      System.out.println(
"Response body: " );
      System.out.println(post.getResponseBodyAsString()); 
      post.releaseConnection(); 
   }
}

6 通过HTTP上传文件

httpclient使用了单独的一个HttpMethod子类来处理文件的上传,这个类就是MultipartPostMethod,该类已经封装了文件上传的细节,我们要做的仅仅是告诉它我们要上传文件的全路径即可,下面的代码片段演示如何使用这个类。

MultipartPostMethod filePost = new MultipartPostMethod(targetURL);
filePost.addParameter(
"fileName" , targetFilePath);
HttpClient
client = new HttpClient();

// 由于要上传的文件可能比较大 , 因此在此设置最大的连接超时时间
client.getHttpConnectionManager(). getParams().setConnectionTimeout(5000);
int status = client.executeMethod(filePost);

上面代码中,targetFilePath即为要上传的文件所在的路径。

7 访问启用认证的页面

我们经常会碰到这样的页面,当访问它的时候会弹出一个浏览器的对话框要求输入用户名和密码后方可,这种用户认证的方式不同于我们在前面介绍的基于表单的用户身份验证。这是HTTP的认证策略,httpclient支持三种认证方式包括:基本、摘要以及NTLM认证。其中基本认证最简单、通用但也最不安全;摘要认证是在HTTP 1.1中加入的认证方式,而NTLM则是微软公司定义的而不是通用的规范,最新版本的NTLM是比摘要认证还要安全的一种方式。

下面例子是从httpclientCVS服务器中下载的,它简单演示如何访问一个认证保护的页面:

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.GetMethod;

public class BasicAuthenticationExample { 

   public BasicAuthenticationExample() { 
   }

   public static void main(String[] args) throws Exception {
      HttpClient client =
new HttpClient();
      client.getState().setCredentials(
"www.verisign.com" , "realm" , new UsernamePasswordCredentials( "username" , "password" ) );

      GetMethod get =
new GetMethod( "https://www.verisign.com/products/index.html" );
      get.setDoAuthentication(
true );
      int status = client.executeMethod( get );
      System.out.println(status+
"\n" + get.getResponseBodyAsString());
      get.releaseConnection();
   }
}

8 多线程模式下使用httpclient

多线程同时访问httpclient,例如同时从一个站点上下载多个文件。对于同一个HttpConnection同一个时间只能有一个线程访问,为了保证多线程工作环境下不产生冲突,httpclient使用了一个多线程连接管理器的类:MultiThreadedHttpConnectionManager,要使用这个类很简单,只需要在构造HttpClient实例的时候传入即可,代码如下:

MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();

HttpClient client = new HttpClient(connectionManager);

以后尽管访问client实例即可。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics