⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpclientest.java

📁 这是一个关于J2EE的开源包common里的许多组件的示例应用程序,可以借鉴.
💻 JAVA
字号:
/**
 * Title : Base Dict Class
 * Description : here Description is the function of class, here maybe multirows    
 * @author        kevin
 * @Version       1.0 
 */

package httpsClient;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringBufferInputStream;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;

/**
 * Class description goes here.
 * @version 1.0  2006-3-20 
 * @author kevin
 */
public class HttpClientest
{

	public static void main(String[] args)
	{
		// 构造HttpClient的实例
		String url = "https://legaltest.o-i.com/ACMBWeb/PrintVendorServlet?vendorId=47446&stateName=test";
		//String url = "https://192.168.121.20:7002/ACMBWeb/PrintVendorServlet?vendorId=75021&stateName=test";
		//String url = "http://192.168.121.20:7008/ACMBWeb/PrintVendorServlet?vendorId=75021&stateName=test";

		Protocol myhttps = new Protocol("https", new MySecureProtocolSocketFactory(), 443);
		Protocol.registerProtocol("https", myhttps);
		// 构造HttpClient的实例
		HttpClient httpClient = new HttpClient();
		// 创建GET方法的实例
		GetMethod getMethod = new GetMethod(url);
		// 使用系统提供的默认的恢复策略
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
		try
		{
			// 执行getMethod
			int statusCode = httpClient.executeMethod(getMethod);
			if(statusCode != HttpStatus.SC_OK)
			{
				System.err.println("Method failed: " + getMethod.getStatusLine());
			}
			// 读取内容
			byte[] responseBody = getMethod.getResponseBody();
			// 处理内容

			viewPdf(responseBody);
			//System.out.println(new String(responseBody));
		}
		catch(HttpException e)
		{
			System.out.println("Please check your provided http address!");
			// 发生致命的异常,可能是协议不对或者返回的内容有问题
			e.printStackTrace();
		}
		catch(IOException e)
		{
			// 发生网络异常
			e.printStackTrace();
		}
		finally
		{
			// 释放连接
			getMethod.releaseConnection();
		}

	}

	public static void viewPdf(byte[] bytes) throws IOException
	{
		File f = new File("d:\\test.pdf");
		if(!f.exists())
		{
			f.createNewFile();
		}

		FileOutputStream fos = new FileOutputStream(f);
		fos.write(bytes);
		fos.close();
		String[] cmd = {"C:\\Program Files\\Adobe\\Acrobat 6.0\\Reader\\AcroRd32.exe", "d:\\test.pdf"};
		Process ps = Runtime.getRuntime().exec(cmd);

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -