日期:2014-05-20  浏览次数:20983 次

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

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

------解决方案--------------------
返回 200 代表就连接成功了啊
------解决方案--------------------
不懂帮顶
------解决方案--------------------
顶一下了,再顶一下 回复内容太短了
------解决方案--------------------
可以将用户名和密码写在URL中,用username= &password=
------解决方案--------------------
HttpURLConnection.HTTP_OK==httpConn.getResponseCode();
它们相等,就是成功连接!
------解决方案--------------------
呵呵,没人啊,再帮顶
------解决方案--------------------
HttpURLConnection.HTTP_OK==httpConn.getResponseCode();
它们相等,就是成功连接!
------解决方案--------------------
调用connect方法,没有捕获到异常就是连接上了
------解决方案--------------------
dddddddddddddddddd
------解决方案--------------------
不懂,帮顶下。
------解决方案--------------------
楼主 我关注你
------解决方案--------------------
获取返回值 是200就成功

------解决方案--------------------
建议使用httpclient类
------解决方案--------------------
网上才查查有例子
很容易实现
------解决方案--------------------
可以看下这个:
Java code
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());
  }
}