Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 软件时空 > 软件相关 > 用java访问web
【标  题】:用java访问web
【关键字】:java,web
【来  源】:http://blog.chinaunix.net/article.php?articleId=62883&blogId=4353

用java访问web

Your Ad Here

以前写过一些通过程序访问网站的应用,但是很可惜,只能用于windows平台.

一直都想找跨平台的方法,今天被我找到了.那就是apache的httpclient 3.0

apache的东东还真是丰富,居然提供了一个java写的库,可以方便我们开发访问web网站的应用.

今天利用httpclient写了一个小程序.该程序可以访问网站的首页.特别支出在于,它支持NTLM身份验证. 因为,某些网站,如果不能够提供有效的身份验证,是无法看到任何信息的.

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.*;

public class HttpClientTutorial {
 
  private static String url = "http://10.8.0.219/";

  public static void main(String[] args) {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    NTCredentials defaultcreds = new NTCredentials("mac-username", "mac-password","mac-hostname","mac-domain");
    client.getState().setCredentials(new AuthScope("10.8.0.219", 80, AuthScope.ANY_REALM), defaultcreds);
   
   
    // Create a method instance.
    GetMethod method = new GetMethod(url);
   
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
      new DefaultHttpMethodRetryHandler(3, false));

    try {
      // Execute the method.
      int statusCode = client.executeMethod(method);

      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
      }

      // Read the response body.
      byte[] responseBody = method.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not binary data
      System.out.println(new String(responseBody));

    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      method.releaseConnection();
    } 
  }
}

编译的时候需要注意,它用到了apache提供的httpclient库,所以要这样做:

javac -cp "/home/sb/commons-httpclient-3.0.jar" HttpClientTutorial.java

运行的时候更加复杂

java -cp "/home/sb/commons-httpclient-3.0.jar:/home/sb/commons-codec-1.3.jar:/home/sb/commons-logging.jar:." HttpClientTutorial

这些jar文件都可以从apache.org下载.

表单提交时提示正在发送:【上一篇】
Python版猜数字游戏:【下一篇】
【相关文章】
  • 在JAVA中实现C++代码的复用
  • WEBLOGIC for HP-UN安装
  • Weblogic6.1 安装 for HP-UNIX 以及注意细节
  • 再说精度-用于显示的数字出现精度问题的处理-(Perl/Java)
  • 发起perl的web浏览器项目,征召人手
  • BEA Weblogic Portal内容管理的几点开发经验
  • JAVA开发环境选择与配置
  • 从后台得到webshell技巧大汇总
  • Donews WebSite PageRank Analysis
  • 腾讯-移动kjava开发
  • 【随机文章】
  • show modem operational-status
  • 40°板状天线 TDJ-2400BKC14
  • C# 3.0 Orcas 简介 (转)
  • Html——Object属性
  • ubuntu dapper en_US.UTF-8 locale 下安装fcitx
  • 我的日构建脚本(进行中)
  • Clementine Application Templates(CATs)介绍
  • C语言位运算的学习
  • JavaScript实现在线编辑表格
  • 了解各种性能参数,用好液晶显示器
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.