Java新手,用httpclient写了一个程序,出现以下错误

Diudiu_2 2015-08-06 11:29:56
a.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:286)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at getweb.Getweb.downloadpage(Getweb.java:29)
at getweb.Getweb.main(Getweb.java:51)




源代码如下:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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.PostMethod;

public class Getweb {
private static HttpClient httpClient = new HttpClient();
static {
httpClient.getHostConfiguration().setProxy("192.168.191.1", 8080);
}

public static boolean downloadpage(String path) throws HttpException,
IOException {
InputStream input = null;
OutputStream output = null;
PostMethod postMethod = new PostMethod(path);
NameValuePair[] postData = new NameValuePair[2];
postData[0] = new NameValuePair("name", "baidu");
postData[1] = new NameValuePair("password", "123456");
postMethod.addParameters(postData);
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_OK) {
input = postMethod.getResponseBodyAsStream();
String filename = path.substring(path.lastIndexOf("/") + 1);
output = new FileOutputStream(filename);
int tempByte = -1;
while ((tempByte = input.read()) > 0) {
output.write(tempByte);
}
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
return true;
}
return false;
}

public static void main(String[] args) {
try {
Getweb.downloadpage("https://www.baidu.com/");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}
...全文
137 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ArayChou 2015-08-06
  • 打赏
  • 举报
回复
connection refuse,访问不到目的IP端口。 先确保你能正常访问
ArayChou 2015-08-06
  • 打赏
  • 举报
回复
httpClient.getHostConfiguration().setProxy("192.168.191.1", 8080); 你这个代理服务器工作正常吧?
Diudiu_2 2015-08-06
  • 打赏
  • 举报
回复
引用 3 楼 tianfang 的回复:
为什么用POST方法提交,应该使用get方法下载普通网页 另外 http://www.baidu.com 会返回503,跳转到https,需要换一个做测试
引用 3 楼 tianfang 的回复:
为什么用POST方法提交,应该使用get方法下载普通网页 另外 http://www.baidu.com 会返回503,跳转到https,需要换一个做测试
改成get方法以后,出现八月 06, 2015 3:15:34 下午 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry 信息: I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect 八月 06, 2015 3:15:34 下午 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry 信息: Retrying request 八月 06, 2015 3:15:55 下午 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry 信息: I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect 八月 06, 2015 3:15:55 下午 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry 信息: Retrying request java.io.FileNotFoundException: at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.<init>(FileOutputStream.java:213) at java.io.FileOutputStream.<init>(FileOutputStream.java:101) at getweb.Getweb.downloadpage(Getweb.java:29) at getweb.Getweb.main(Getweb.java:47)
tianfang 2015-08-06
  • 打赏
  • 举报
回复
为什么用POST方法提交,应该使用get方法下载普通网页 另外 http://www.baidu.com 会返回503,跳转到https,需要换一个做测试
Diudiu_2 2015-08-06
  • 打赏
  • 举报
回复
八月 06, 2015 2:27:17 下午 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry 信息: I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect 八月 06, 2015 2:27:17 下午 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry 信息: Retrying request 八月 06, 2015 2:27:37 下午 org.apache.commons.httpclient.HttpMethodBase readResponseBody 信息: Response content length is not known 改了端口之后,又出现这样的错误?

50,451

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧