浏览 1178 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2007-11-26 关键字: HttpClient
写了一个关于httpClient的小例子,但是怎么都不能正常运行 各位高人帮忙诊断一下 谢谢!
package com.cstp.reptile.utils;
import java.io.IOException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class SimpleHttpClient {
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
// 执行postMethod
int statusCode = client.executeMethod(getPostMethod());
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||
statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = getPostMethod().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.");
}
return;
}
}
/**
* 使用POST方式提交数据
*
* @return
*/
private static HttpMethod getPostMethod() {
String url = "http://search.51job.com/jobsearch/keyword_search.php";
PostMethod post = new PostMethod(url);
NameValuePair keyword1 = new NameValuePair("keyword1", "软件工程师 java");
NameValuePair keyword = new NameValuePair("keyword", "软件工程师 java");
NameValuePair jobarea = new NameValuePair("jobarea", "0100");
NameValuePair keywordtype = new NameValuePair("keywordtype", "2");
post.setRequestBody(new NameValuePair[] { keyword1, keyword, jobarea, keywordtype });
return post;
}
}
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2007-11-26
错误信息如下
2007-11-26 21:07:02 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry 信息: I/O exception (org.apache.commons.httpclient.NoHttpResponseException) caught when processing request: The server search.51job.com failed to respond 2007-11-26 21:07:02 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry 信息: Retrying request |
|
| 返回顶楼 | |
|
时间:2007-11-26
该请求有返回。代码是200.内容打印出来是正确的网页内容。
代码第21行对getPostMethod的调用让人看不明白。 |
|
| 返回顶楼 | |
|
时间:2007-11-27
李超群 写道 该请求有返回。代码是200.内容打印出来是正确的网页内容。
代码第21行对getPostMethod的调用让人看不明白。 内容打印出来是正确的内容? 我这里总是出异常....觉得很奇怪 ..... |
|
| 返回顶楼 | |
|
时间:2007-11-28
这个是我用的类库
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/> <classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/> <classpathentry kind="lib" path="lib/commons-logging-1.1.jar"/> <classpathentry kind="output" path="bin"/> </classpath> |
|
| 返回顶楼 | |


