请考虑以下代码片段:
URL url = new URL("https://wfs.geodienste.ch/av/deu?&LAYERS=LCSFC,SOLI,SOSFC,LOCPOS,HADR,LNNA,OSNR,RESF,OSBP,MBSF&FORMAT=image%2Fjpeg&DPI=100&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&SRS=EPSG%3A2056&BBOX=2637158.3069220297,1236087.7425729688,2639935,1237632&WIDTH=2463&HEIGHT=1369");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
InputStream is = uc.getInputStream();
对于给定的 URL,我收到 401 异常:
java.io.IOException: Server returned HTTP response code: 401 for URL: https://wfs.geodienste.ch/av/deu?&LAYERS=LCSFC,SOLI,SOSFC,LOCPOS,HADR,LNNA,OSNR,RESF,OSBP,MBSF&FORMAT=image%2Fjpeg&DPI=100&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&SRS=EPSG%3A2056&BBOX=2637158.3069220297,1236087.7425729688,2639935,1237632&WIDTH=2463&HEIGHT=1369
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1839)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at HTTPTestMain.doit(HTTPTestMain.java:43)
at HTTPTestMain.main(HTTPTestMain.java:28)
现在,我希望 URL 在与浏览器一起使用时要求提供一些凭据。令人惊讶的是,不需要凭据,例如 Firefox 中的响应是 200。
出于好奇,我添加了以下代码行:
uc.setRequestProperty("Authorization", "Basic ");
仍然是相同的 401 响应。
你能告诉我用 Java 获得正确的响应需要什么吗?
RISEBY
PIPIONE
相关分类