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

📄 testclient.java

📁 简单的java http 服务器和客户端源代码
💻 JAVA
字号:
package testHttp.httpclient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

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.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * 
 * <p>
 * Title: TestClient.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright:OnewaveInc Copyright (c) 2007
 * </p>
 * <p>
 * Company: OnewaveInc
 * </p>
 * 
 * @author Zhengrw
 * @version 3.0
 */
public class TestClient {

	public void testGet() {
		HttpClient client = new HttpClient();
		GetMethod getMethod = new GetMethod("http://www.ibm.com");
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());
		try {
			int statusCode = client.executeMethod(getMethod);
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: "
						+ getMethod.getStatusLine());
			}
			byte[] responseBody = getMethod.getResponseBody();
			System.out.println(new String(responseBody));
		} catch (HttpException e) {

		} catch (IOException e) {

		} finally {
			getMethod.releaseConnection();
		}
	}

	public void testConfig() throws UnsupportedEncodingException, IOException {
		HttpClient client = new HttpClient();
		client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");
		PostMethod method = new PostMethod("");// 使用 POST 方式提交数据
		client.executeMethod(method); // 打印服务器返回的状态
		System.out.println(method.getStatusLine()); // 打印结果页面
		String response = new String(method.getResponseBodyAsString().getBytes(
				"8859_1"));
		// 打印返回的信息
		System.out.println(response);
		method.releaseConnection();

	}

	public void testXMLPost() throws HttpException, IOException {
		File input = new File("F://test.xml");
		PostMethod post = new PostMethod(
				"http://localhost:8180");
		// 设置请求的内容直接从文件中读取
		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=\"UTF-8\"");
		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();
	}

	public void testPost() {
		HttpClient client = new HttpClient();
		String url = "http://www.newsmth.net/bbslogin2.php";
		PostMethod postMethod = new PostMethod(url);
		NameValuePair[] data = { new NameValuePair("id", "btfeifei"),
				new NameValuePair("passwd", "19841028") };
		postMethod.setRequestBody(data);
		int statusCode;
		try {
			statusCode = client.executeMethod(postMethod);
			if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
					|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
				Header locationHeader = postMethod
						.getResponseHeader("location");
				String location = null;
				if (locationHeader != null) {
					location = locationHeader.getValue();
					System.out
							.println("The page was redirected to:" + location);
				} else {
					System.err.println("Location field value is null.");
				}
			} else if (statusCode == HttpStatus.SC_OK) {
				byte[] responseBody = postMethod.getResponseBody();
				System.out.println(new String(responseBody));
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestClient tc = new TestClient();
		try {
			tc.testXMLPost();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

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