我正在获取连接超时:当我在url中使用用户名和密码时连接异常。网址是" http://testadmin:testadmin@myhostName/manager/text/list";
这个网址在chrome,firefox网页浏览器中工作,但是当我打算通过java代码访问这个网址时。
以下是我得到的输出和异常:
&&&&&&&&&&&&&&&
*********88
response
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at test.TomcatTest.main(TomcatTest.java:23)
我使用以下代码。
package test;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class TomcatTest {
public static void main(String[] args) {
String listUrl = "http://testadmin:testadmin@myhostName/manager/text/list";
String serverResponse = "";
URL url = null;
try {
System.out.println("&&&&&&&&&&&&&&&");
url = new URL(listUrl);
URLConnection connection = url.openConnection();
System.out.println("*********88");
// i also tried without setting readtimeout.
connection.setReadTimeout(3 * 60 * 1000);// set timeout 3 minutes
InputStream inputStream = connection.getInputStream();
System.out.println("^^^^^^^^^^^^^");
int chr = -1;
while ((chr = inputStream.read()) != -1) {
System.out.print((char)chr);
serverResponse += (char)chr;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("response " + serverResponse);
}
}
答案 0 :(得分:1)
URL http://<user>:<pass>@url
不会以这种方式发送到Web服务器。浏览器从网址中取出用户名和密码,并创建一个basic authentication标头。
Java字面上将URL发送到服务器。这是一个安全问题,因为URL可能会在几个阶段记录。
此演示代码99%基于answer by Wanderson Santos and joel234和Java 8 Base64自适应:
String _url_string = "http://server/"
url = new URL(_url_string);
URLConnection uc = url.openConnection();
String userpass = _user + ":" + _password;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
InputStream in = uc.getInputStream();
答案 1 :(得分:0)
现在我有一个解决方案,我们不能像上面那样在网址中使用用户名和密码。
可以像这样使用:
String listUrl = "http://myHostName/manager/text/list";
//use username and password here
connection.setRequestProperty("Authorization", String.format("Basic %s", new BASE64Encoder().encode("testadmin:testadmin".getBytes("UTF-8"))));