String json = URLEncoder.encode(jsonStr, "UTF-8");
HttpClient httpclient = getClient(true);//设置代理服务器(方法如下)
postMethod = new PostMethod(_url + "company&dataType=companyData"); //传送的是json的数据连接
StringRequestEntity requestEntity = new StringRequestEntity(json);
postMethod.setRequestEntity(requestEntity);
int code = httpclient.executeMethod(postMethod);
//服务器代理方法
private HttpClient getClient(boolean flag) {
HttpClient client = new HttpClient();
if (flag) {// true
String proxyHost = "pc1111101";// 代理主机
Integer proxyPort = 8080;// 端口
client.getHostConfiguration().setProxy(proxyHost, proxyPort);
String userName = "abc";// 用户名
String password = "abc123";// 密码
Credentials defaultcreds = new UsernamePasswordCredentials(
userName, password);
client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
}
return client;
}
当运行到这里的 int code = httpclient.executeMethod(postMethod); 时候就会出以下异常:java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:235)
at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77)
at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105)
at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1115)
at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1832)
at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
备注:我用的是commons-httpclient-3.1的包
java.net.SocketException: Connection reset 异常
答案:4 悬赏:50 手机版
解决时间 2021-01-29 21:27
- 提问者网友:人生佛魔见
- 2021-01-29 06:57
最佳答案
- 五星知识达人网友:我住北渡口
- 2021-01-29 08:35
控制台输入
telnet pc1111101 8080
如果进去的话表示服务成功了,否则失败。
楼主应该是地址写错了。pc1111101 换成ip 试试
telnet pc1111101 8080
如果进去的话表示服务成功了,否则失败。
楼主应该是地址写错了。pc1111101 换成ip 试试
全部回答
- 1楼网友:刀戟声无边
- 2021-01-29 11:46
我也觉得
- 2楼网友:走死在岁月里
- 2021-01-29 10:45
我运行你的代码,结果如下:
程序等待服务器端输入数据并且输出,可见没有抛出你的错误。请检查你的防火墙是否允许这个9999端口进行通讯。- 3楼网友:未来江山和你
- 2021-01-29 09:26
应该系你那个post被占用了,所以一直从新连接
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯