怎么知道服务器上的网站是否连通,使用HttpURLConnection连接http服务器,怎么判断是否连接成功...

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

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

Java codeimport 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服务器成功

------解决方案--------------------

返回 200 代表就连接成功了啊

------解决方案--------------------

不懂帮顶

------解决方案--------------------

顶一下了,再顶一下 回复内容太短了

------解决方案--------------------

可以将用户名和密码写在URL中,用username= &password=

------解决方案--------------------

HttpURLConnection.HTTP_OK==httpConn.getResponseCode();

它们相等,就是成功连接!

------解决方案--------------------

呵呵,没人啊,再帮顶

------解决方案--------------------

HttpURLConnection.HTTP_OK==httpConn.getResponseCode();

它们相等,就是成功连接!

------解决方案--------------------

调用connect方法,没有捕获到异常就是连接上了

------解决方案--------------------

dddddddddddddddddd

------解决方案--------------------

不懂,帮顶下。

------解决方案--------------------

楼主 我关注你

------解决方案--------------------

获取返回值 是200就成功

------解决方案--------------------

建议使用httpclient类

------解决方案--------------------

网上才查查有例子

很容易实现

------解决方案--------------------

可以看下这个:

Java codeimport 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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值