如何使用java从Https Url获取响应代码(How to get response code from a Https Url using java)

编程入门 行业动态 更新时间:2024-10-25 09:31:42
如何使用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

更多推荐

本文发布于:2023-07-30 07:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   代码   java   Url   Https

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!