java - Java - 当我使用jsoup库请求URL时发生错误的响应代码

我正在开发WI Fi应用程序以连接Hotspot网络,

我想接收基于ssid状态的响应代码,如果SSID处于auth状态,则响应为302如果SSID处于connect状态,则响应为200,这就是我想要的。

源代码:


try{


 URL url = new URL("http://microsenseindia.com");


 Connection.Response response = Jsoup.connect(String.valueOf(url)).timeout(10000).followRedirects(false).execute();


 //Connection.Response response = Jsoup.connect("http://microsenseindia.com").timeout(10000).execute();


 Log.d(TAG,"check_if_internet_available :" + response.statusCode());


} catch(Exception e) {


 Log.e(TAG,"Jsoup_Exception:"+e);


}



时间:

首先代码http 302

6.4.3.302 Found

302 (已找到)状态代码表示目标资源暂时位于其他URI下,

对于你的问题,以下代码可以在不使用url定义的情况下帮助你:


 try{


 JsoupConnection.Response response = Jsoup.connect("http://microsenseindia.com")


 .userAgent("Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like 


 Gecko) Chrome/19.0.1084.46 Safari/536.5")


 .timeout(100000)


 .ignoreHttpErrors(true) 


 .execute();


 Log.d(TAG,"check_if_internet_available :" + response.statusCode());


 }catch(Exception e){


 Log.e(TAG,"Jsoup_Exception:"+e);


}



也许你应该更改方法并使用HttpURLConnection


URL url = new URL("http://microsenseindia.com");


HttpURLConnection connection = (HttpURLConnection)url.openConnection();


connection.setRequestMethod("GET");


connection.connect();



int statusCode= connection.getResponseCode();




 try {


 while (true) {


 sleep(1000);


 final URL url;


 try {


 url = new URL("http://microsenseindia.com");


 HttpURLConnection huc = (HttpURLConnection) url.openConnection();


 huc.setInstanceFollowRedirects(true);


 huc.setRequestMethod("POST");


 huc.connect();


 huc.setReadTimeout(5000);


 huc.setConnectTimeout(5000);


 int responseCode = huc.getResponseCode();


 Log.d(TAG,"responseCode:"+responseCode);


 } catch (Exception e) {


 e.printStackTrace();


 }



这是我正在使用的http连接方法。

...