使用HttpURLConnection连接http服务器,如何判断是否连接成功

paodan 2010-03-01 07:15:21
使用HttpURLConnection连接http服务器,调用connect方法,如果服务器ip和端口正确,无论服务器是否有用户名和密码,URLConnection的connected字段就会返回true,没有判断用户名和密码,是不是HttpURLConnection只发送一个数据包到指定服务器,发现指定的端口打开,connected字段就会返回true?

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public static void testConnect(String surl) throws IOException {
URL url = new URL(surl);
String userInfo = url.getUserInfo();
if (userInfo != null && userInfo.length() > 0) {
String string = surl.replace(userInfo, "");
url = new URL(string);
}
URLConnection connection = (HttpURLConnection) url.openConnection();

if (userInfo != null && userInfo.length() > 0)
connection.setRequestProperty("Authorization", userInfo);
connection.connect();


}
public static void main(String args[]) throws IOException{
String url = "http://192.9.168.11:1122/";//http://user:pass@192.9.168.11:1122/
testConnect(url);
}

请教如何根据HttpURLConnection返回的值判断连接http服务器成功
...全文
6021 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
louis062 2011-10-12
  • 打赏
  • 举报
回复
System.out.println("state code:" + con.getResponseCode());
我这里直接出错。没打印出来
yjwen0057 2011-10-11
  • 打赏
  • 举报
回复
谢谢35楼,用35楼的方法可以通过认证,我之前用HttpClient 认证一直没有成功。
st50886160 2010-04-13
  • 打赏
  • 举报
回复
服务器端是你控制吗?
如果是你控制建议服务器往输出流里面写一些相关数据代表验证通过

不这样做我觉得很难代表是否验证通过(它也只是判断是否连接成功而已)
paodan 2010-04-02
  • 打赏
  • 举报
回复
楼下的要加油啊
paodan 2010-04-02
  • 打赏
  • 举报
回复
谢谢楼上各位兄弟,特别感谢35楼的兄弟
paodan 2010-04-02
  • 打赏
  • 举报
回复
[Quote=引用 35 楼 thc1987 的回复:]
可以看下这个:

Java code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.PasswordAuthentication;
import j……
[/Quote]这个可以,稍微修改一下就行,还有需要设置一下流的字符编码,html中带有汉字的连接显示乱码,因此需要根据不同的页面设置编码。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.PasswordAuthentication;
import java.net.URL;


/**
* TODO javadoc for http.AuthenticatorTest
*
* @author wanghan
*/
public class AuthenticatorTest {
public static void main(String[] argv) throws Exception {
Authenticator.setDefault(new MyAuthenticator("user","passwd"));
URL url = new URL("http://192.168.200.10:8002");

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));//这里根据服务器编码设置
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}


}
class MyAuthenticator extends Authenticator {
private String name;
private String passwd;
public MyAuthenticator(String name,String passwd){
this.name = name;
this.passwd = passwd;
}

protected PasswordAuthentication getPasswordAuthentication() {
String promptString = getRequestingPrompt();
System.out.println(promptString);
String hostname = getRequestingHost();
System.out.println(hostname);
InetAddress ipaddr = getRequestingSite();
System.out.println(ipaddr);
//int port = getRequestingPort();

String username = name;
String password = passwd;
return new PasswordAuthentication(username, password.toCharArray());
}
}


猿敲月下码 2010-04-02
  • 打赏
  • 举报
回复
可以看下这个:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.PasswordAuthentication;
import java.net.URL;

public class Test {
public static void main(String[] argv) throws Exception {
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL("http://hostname:80/index.html");

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
}

class MyAuthenticator extends Authenticator {

protected PasswordAuthentication getPasswordAuthentication() {
String promptString = getRequestingPrompt();
System.out.println(promptString);
String hostname = getRequestingHost();
System.out.println(hostname);
InetAddress ipaddr = getRequestingSite();
System.out.println(ipaddr);
int port = getRequestingPort();

String username = "name";
String password = "password";
return new PasswordAuthentication(username, password.toCharArray());
}
}
paodan 2010-04-02
  • 打赏
  • 举报
回复
[Quote=引用 33 楼 thc1987 的回复:]
Java code
public static boolean isConnectedUrl(String url) {
HttpURLConnection con;
try {
HttpURLConnection.setFollowRedirects(false);
con = (HttpURLConnec……
[/Quote]谢谢,这个方法用过,对于需要认证的Http服务器url格式是怎么样的?比如http://192.168.200.9:8002,用户名和密码为user/paswwd,试过这样的http://user:passwd@192.168.200.9:8002,但是返回了401,连接失败。不知道如何发送认证信息。
paodan 2010-04-01
  • 打赏
  • 举报
回复
真纠结
猿敲月下码 2010-04-01
  • 打赏
  • 举报
回复
public static boolean isConnectedUrl(String url) {
HttpURLConnection con;
try {
HttpURLConnection.setFollowRedirects(false);
con = (HttpURLConnection) new URL(url)
.openConnection();
con.setRequestMethod("HEAD");
System.out.println("state code:" + con.getResponseCode());
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
return true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return false;
}
shuaiAWP 2010-04-01
  • 打赏
  • 举报
回复
网上才查查有例子
很容易实现
shuaiAWP 2010-04-01
  • 打赏
  • 举报
回复
建议使用httpclient类
paodan 2010-03-16
  • 打赏
  • 举报
回复
因此想请教一下如果http服务器需要认证的,如何传递认证信息(用户名和密码),除了返回200,还会返回什么有用的信息(与不需要认证的区别信息)?
paodan 2010-03-16
  • 打赏
  • 举报
回复
这里再说明一下,可以参考一下7楼的代码,对于Http服务器(IIS或者Apache等),如果url和端口正确,都会返回200,也就是说不管Http服务器是否需要用户认证,只要是http://url:端口正确,返回码都会是200.
quansheng2580 2010-03-16
  • 打赏
  • 举报
回复
获取返回值 是200就成功
macrotea-cn 2010-03-16
  • 打赏
  • 举报
回复
楼主 我关注你
xiesisi3 2010-03-16
  • 打赏
  • 举报
回复
不懂,帮顶下。
「已注销」 2010-03-16
  • 打赏
  • 举报
回复
dddddddddddddddddd
paodan 2010-03-15
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 taozhaocailiu 的回复:]
HttpURLConnection.HTTP_OK==httpConn.getResponseCode();
它们相等,就是成功连接!
[/Quote]这只是检测url和端口是否正确,如果有用户名和密码的如何连接,即如何发送用户名和密码?
重返春季 2010-03-15
  • 打赏
  • 举报
回复
HttpURLConnection.HTTP_OK==httpConn.getResponseCode();
它们相等,就是成功连接!
加载更多回复(21)
第一种方法 使用标准的JAVA接口 1 将用户名和密码等用Map泛型封装 再使用StringBuffer 转换成一串字符串 然后新建URL打开openConnection 得到 httpURLConnection 设置最长连接时间和setRequestMethod请求方法 用GET还是POST 提交数据用POST 打开输入和输出流 获取上传信息 字节大小以及长度 设置请求体的类型是文本类型 获得输出流 向服务器输出数据 获得服务器响应的结果和状态码 如果 返回码等于200 得到服务器返回的输入流 将输入流转换成指定编码的字符串并返回 就可以成功提交并得到服务器返回的信息 第二种方法 使用标准Apache接口 02 03 15 19 30 + 06 12 02 03 15 19 30 31 + 06 12 1 将用户名和密码等用Map泛型封装 再使用List<NameValuePair> list new ArrayList<NameValuePair> ; 转换成一串字符串 然后新建URL打开openConnection 得到 httpURLConnection 设置最长连接时间和setRequestMethod请求方法 用GET还是POST 提交数据用POST 打开输入和输出流 获取上传信息 字节大小以及长度 设置请求体的类型是文本类型 获得输出流 向服务器输出数据 获得服务器响应的结果和状态码 如果 返回码等于200 得到服务器返回的输入流 将输入流转换成指定编码的字符串并返回 就可以成功提交并得到服务器返回的信息">第一种方法 使用标准的JAVA接口 1 将用户名和密码等用Map泛型封装 再使用StringBuffer 转换成一串字符串 然后新建URL打开openConnection 得到 httpURLConnection 设置最长连接时间和setRequestMethod请求方法 用GET还是POST 提交数据用POST [更多]

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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