?? testclient.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 + -