httpunit

jar包下载地址 : http://sourceforge.net/projects/httpunit/

涉及的jar包 :

js-1.6R5.jar,

nekohtml-0.9.5.jar,

utilities.jar,

xercesImpl-2.8.1.jar,

xpp3_min-1.1.3.4.O.jar,

xstream-1.2.2.jar,

httpunit.jar

参考资料http://syab11.iteye.com/blog/611746

基本的一些代码

package tools;



import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.util.Map;

import com.meterware.httpunit.GetMethodWebRequest;

import com.meterware.httpunit.PostMethodWebRequest;

import com.meterware.httpunit.WebConversation;

import com.meterware.httpunit.WebResponse;



public class HttpTools

{

    public static void post(String url, String xml, Map<String, String> header)

    {

        // 模拟浏览器

        ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());

        post(url, in, header);

        

    }

    

    private static void post(String url, InputStream in, Map<String, String> header)

    {

        // 模拟浏览器

        WebConversation web = new WebConversation();

        

        // 设置代理

        // web.setProxyServer(proxyHost, proxyPort, userName, password)

        // post数据传输

        PostMethodWebRequest post = new PostMethodWebRequest(url, in, "text/xml");

        for (Map.Entry<String, String> entry : header.entrySet())

        {

            post.setHeaderField(entry.getKey(), entry.getValue());

        }

        try

        {

            // 执行post放回response对象

            WebResponse response = web.getResponse(post);

            System.out.printf("text : %s", response.getText());

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

    }

    

    public static void get(String url, String xml, Map<String, String> header)

    {

        WebConversation web = new WebConversation();

        GetMethodWebRequest get = new GetMethodWebRequest(url);

        for (Map.Entry<String, String> entity : header.entrySet())

        {

            get.setHeaderField(entity.getKey(), entity.getValue());

        }

        

        try

        {

            WebResponse response = web.getResponse(get);

            System.out.printf("text : %s", response.getText());

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

        

    }

}

 

你可能感兴趣的:(Web,xml,.net,浏览器,Blog)