Jakarta的HttpClient API模拟客户端URL请求(批量提交)

使用jar包:commons-httpclient-3.0.1.jar

 

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class Test {

	public static void main(String[] args) {
		System.out.println("静态报表模板ID取得开始...");
		List<String> starepNameList = new ArrayList<String>();
		//工程中存放模板的文件夹,根据实际路径填写
		File f=new File("H:\\FTP\\new_template_updated\\template");
		File[] fs = f.listFiles();
		for(int i=0; i < fs.length; i ++){
			if(fs[i].isDirectory()){
				starepNameList.add((String)fs[i].getName());
			}
		}
		System.out.println("共计模板数:"+starepNameList.size());		
		System.out.println("静态报表模板ID取得结束...");
		System.out.println("----------------------------");
		
		HttpClient client = new HttpClient();
		//client.setConnectionTimeout(100000000);
		//client.setTimeout(100000000);
		client.getHostConfiguration().setHost("10.131.65.16", 6666, "http");
		System.out.println("静态报表模板访问授权开始...");
		PostMethod post1 = new PostMethod("/jsp/common/show_html.jsp?queryStr=1&staffId=LC_LIKE&staffpwd=Qwer1234&cityScopeForDM=H0&countryScopeForDM=0000&cityScope=290&countryScope=0000&areaScope=00000");
		System.out.println("静态报表模板ID取得结束...");
		
		System.out.println("----------------------------");
		
		try {
			client.executeMethod(post1);
		} catch (HttpException e1) {
			System.out.println("静态报表模板访问授权出错...");
			e1.printStackTrace();
		} catch (IOException e1) {
			System.out.println("静态报表模板访问授权出错...");
			e1.printStackTrace();
		}
		System.out.println("共计【"+starepNameList.size()+"】个,循环访问静态报表模板:");
		for(int j=0; j < starepNameList.size(); j ++){
			String fid = (String)starepNameList.get(j);
		    PostMethod post2 = new PostMethod("/jsp/starep/StarepView.do");
		    NameValuePair simcard = new NameValuePair("fid", fid);
		    post2.setRequestBody(new NameValuePair[] { simcard });
		    try {
		    	System.out.println("静态报表模板【ID="+ fid + "】 开始访问...");
				client.executeMethod(post2);				
				System.out.println("静态报表模板【ID="+ fid + "】 已访问完成!   第<"+(j+1)+">个.");
			} catch (HttpException e) {
				System.out.println("静态报表模板【ID="+ fid + "】 访问出错...");
				e.printStackTrace();
			} catch (IOException e) {
				System.out.println("静态报表模板【ID="+ fid + "】 访问出错...");
				e.printStackTrace();
			}
			if(j == (starepNameList.size()-1)){
				System.out.println("****************************");
				System.out.println("所有静态报表模板访问结束!");
				System.out.println("****************************");
				break;
			}
		}
	}
}


 

你可能感兴趣的:(Jakarta的HttpClient API模拟客户端URL请求(批量提交))