Java 测试URL地址是否能正常连接的代码
2个回答
2016-11-14
展开全部
String url = "你的url地址";
URL uri = new URL(url);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(90000);
conn.setReadTimeout(180000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
这个过程就是请求url跳转的,一般都是post方式,当然,这个可以自己选择get也可以,osw就是请求返回的数据了
URL uri = new URL(url);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(90000);
conn.setReadTimeout(180000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
这个过程就是请求url跳转的,一般都是post方式,当然,这个可以自己选择get也可以,osw就是请求返回的数据了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询