如何使用java从Https Url获取响应代码(How to get response code from a Https Url using java)
您好我正在尝试获取https网址的响应代码
import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.io.*; import javax.net.ssl.HttpsURLConnection; import org.apache.commons.codec.binary.Base64; public class test { public static void main(String[] args) throws Exception { /* String userpass = "NUPuMYCGIupzhbIe05cIwH2CcIQHXlh6" + ":" + "keG4sZAP82dcJK8H"; String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); */ String httpsURL = "https://api.devo.com/ad/v1"; URL url = new URL ( httpsURL ); URLConnection connection = url.openConnection(); //connection.setRequestProperty("Authorization", "Basic "+"lVQdU1ZQ0dJdX6aGJJZTAYl3SDJDY0lRSFhsaDY6a2VHNHNaQVA4MmRjSks4SA=="); //String encoded = Base64.encode(username+":"+password); connection.connect(); // Cast to a HttpURLConnection if ( connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; int code = httpConnection.getResponseCode(); System.out.println(code); } else { System.err.println ("error - not a http request!"); } } }没有基本的auth我得到响应代码为401,但当我使用基本身份验证,因为你看到我已经评论了这些
//connection.setRequestProperty("Authorization", "Basic "+"lVQdU1ZQ0ddXB6aGJJZTA1Y0l3SDJDY0lRSFhsaDY6a2VNHNaQVA4MmRjSks4SA==");我得到500作为代码但是当URL下降时我现在得到400如何通过基本身份验证以及为什么我在使用Basic身份验证时获得500作为代码? 请建议我
Hi am trying to fetch the response code of a https url
import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.io.*; import javax.net.ssl.HttpsURLConnection; import org.apache.commons.codec.binary.Base64; public class test { public static void main(String[] args) throws Exception { /* String userpass = "NUPuMYCGIupzhbIe05cIwH2CcIQHXlh6" + ":" + "keG4sZAP82dcJK8H"; String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); */ String httpsURL = "https://api.devo.com/ad/v1"; URL url = new URL ( httpsURL ); URLConnection connection = url.openConnection(); //connection.setRequestProperty("Authorization", "Basic "+"lVQdU1ZQ0dJdX6aGJJZTAYl3SDJDY0lRSFhsaDY6a2VHNHNaQVA4MmRjSks4SA=="); //String encoded = Base64.encode(username+":"+password); connection.connect(); // Cast to a HttpURLConnection if ( connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; int code = httpConnection.getResponseCode(); System.out.println(code); } else { System.err.println ("error - not a http request!"); } } }Without basic auth am getting response code as 401 but when I use basic auth as you see I have commented those out
//connection.setRequestProperty("Authorization", "Basic "+"lVQdU1ZQ0ddXB6aGJJZTA1Y0l3SDJDY0lRSFhsaDY6a2VNHNaQVA4MmRjSks4SA==");I am getting 500 as code but when the URL is down am getting 400 now how can I pass the basic auth and why am getting 500 as code when I pass with Basic auth? Please suggest me
最满意答案
我建议你使用Apache Http Components。 看看这里
I suggest you the Apache Http Components. Take a look here
更多推荐
发布评论