how to find out the response code(200,404,400...) of the URL?
2014-08-21 12:53
465 查看
一 :需求:写一个Junit测试链接可用。
二 :有一些网站经过SSL加密的,所以必须在本地添加秘钥证书,才可以用上面的方式测试URL的连通性。要不然会遇到运行时异常:
javax.net.ssl.SSLException: java.lang.RuntimeException:
Unexpected error: Java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty。
打开你要测试的网址,然后导出网站的SSL证书,c:/crt
用記事本打開類似:
-----BEGIN CERTIFICATE-----
MIIDITCCAoqgAwIBAgIQS6WuWd7dHMeAfIkikfDiQzANBgkqhkiG9w0BAQQFADBM
MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0wNjA1MTUyMzE4MTFaFw0w
j8thQJ7StDhAISTBTx/LE0qPlQLfkT3WQOsRb5sQoW/OkV4w9m0TXhWkLsIYngDD
2DJnR/y4HprZmo7M/3wStwO/UiDPIfTzd90SFfCU+pDV41logQ==
-----END CERTIFICATE-----
System.setProperty("javax.net.ssl.trustStore","c:/crt");
在这个%JAVA_HOME%/bin/keytool.exe有个keytool工具,也是将你网站的证书文件加到本地环境中。
%JAVA_HOME%\bin\keytool
-import -file 'parm1' -keystore 'parm2'
parm1是导入证书的文件名称(crt)
parm1是导入证书的文件所在文件夹(c:/)
导入过程中会提示用户输入密码,这个用户名和密码都是任意的。
public void linksAndWikiPages() throws Exception { List<String> urlList = new ArrayList <String> (); urlList.add(""); try { for(int u=0;u<urlList.size();u++){ System.setProperty("javax.net.ssl.trustStore","C:/crt");//this is point String href = urlList.get(u); selenium.shutDownSeleniumServer(); URL url = new URL(href); URLConnection connection = url.openConnection(); if(connection instanceof HttpURLConnection){ HttpURLConnection httpConnection = (HttpURLConnection) connection; int code = httpConnection.getResponseCode(); assertEquals(code, 200); }else{ System.out.println(); } } } catch (Exception e) { e.printStackTrace(); } }
二 :有一些网站经过SSL加密的,所以必须在本地添加秘钥证书,才可以用上面的方式测试URL的连通性。要不然会遇到运行时异常:
javax.net.ssl.SSLException: java.lang.RuntimeException:
Unexpected error: Java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty。
打开你要测试的网址,然后导出网站的SSL证书,c:/crt
用記事本打開類似:
-----BEGIN CERTIFICATE-----
MIIDITCCAoqgAwIBAgIQS6WuWd7dHMeAfIkikfDiQzANBgkqhkiG9w0BAQQFADBM
MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0wNjA1MTUyMzE4MTFaFw0w
j8thQJ7StDhAISTBTx/LE0qPlQLfkT3WQOsRb5sQoW/OkV4w9m0TXhWkLsIYngDD
2DJnR/y4HprZmo7M/3wStwO/UiDPIfTzd90SFfCU+pDV41logQ==
-----END CERTIFICATE-----
System.setProperty("javax.net.ssl.trustStore","c:/crt");
在这个%JAVA_HOME%/bin/keytool.exe有个keytool工具,也是将你网站的证书文件加到本地环境中。
%JAVA_HOME%\bin\keytool
-import -file 'parm1' -keystore 'parm2'
parm1是导入证书的文件名称(crt)
parm1是导入证书的文件所在文件夹(c:/)
导入过程中会提示用户输入密码,这个用户名和密码都是任意的。
相关文章推荐
- mysql多实例配置下,用脚本启动mysql时,出现Please read "Security" section of the manual to find out how to run mysqld as root!
- Please read "Security" section of the manual to find out how to run mysqld as root!错误解决
- How to find the source code of the special command?
- Please read “Security” section of the manual to find out how to run mysqld as root!错误解决(转)
- Please read "Security" section of the manual to find out how to run mysqld as root!错误解决
- [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as r
- [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
- Please read "Security" section of the manual to find out how to run mysqld as root!错误解决
- [ios] how to findout the language you use on ios devices.
- All-In-One Code Framework(AIO): 如何使用C#编写进程外的COM组件 (How to write an out-of-proc COM server in C#)
- How to find out the Sql Server version and service pack
- How to change the URL of your SVN repository
- How to code the forward declaration of class or struct using template?
- How to find child controls that are located in the template of a parent control
- In C# how to find the mime type of a file
- How to open the dialog out of iframe by using jquery dialog
- Weblogic 10g: How to find out JNDI name of EJB Session Bean
- 如何找出相邻3条记录都满足同一条件(How to find out 3 continuous records all reach the same condition)
- How-To Find the Source of "Unaligned Access"
- How to calculate the time consumed on a block of souce code.