-1

Please consider the following code snippet:

    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();

For the given URL I get a 401 Exception:

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)

Now I'd expect the URL to ask for some credentials when using it with a browser. Surprisingly there are no credentials needed and the response in e.g. Firefox is 200.

Just for curiousity I've added the following line of code:

uc.setRequestProperty("Authorization", "Basic ");

Still the same 401 response.

Can you tell me what's needed to get the Response right with Java?

Kind regards Klib

klib009
  • 263
  • 5
  • 18
  • You have to set the credentials for Basic athentication https://stackoverflow.com/a/5137446/3636601 – Jens Nov 07 '18 at 12:23

2 Answers2

0

401 means "Unauthorized", so there must be something with your credentials.You could use an Authenticator

enter code here
    Authenticator.setDefault(new Authenticator(){

@Override
    protected PasswordAuthentication getPasswordAuthentication(){          
       return new PasswordAuthentication(login, password.toCharArray());
    }
});
Muhammad_08
  • 140
  • 2
  • 12
0

I was also able to access that page from a browser without any password.

It is possible that they are treating requests from a web browser differently from programmatic requests; i.e. from an automated scraper. For example, they may be looking at the "User-Agent" header in your requests.

But if you try to reverse engineer this to evade possible restrictions, they may decide to block you using other mechanisms.

I think you need to contact the site's support: https://geodienste.ch/support. Ask them how to deal with this. Find out if you need an account, and how to get one.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for your feedback. I've already conctacted the owner. They say 'not auth required'. We use some services (on the same domain) which require authentication and the problem remains the same. Other domains which require authentications do work with uc.setRequestProperty("Authorization", "Basic " + encode(username":"+new sun.misc.BASE64Encoder().encode(password.getBytes()))); – klib009 Nov 07 '18 at 13:24
  • OK, then I suggest you test to see if it is the User-Agent header. If necessary, use wireshark or similar to compare the requests your java app is sending with the requests that your browser is sending. – Stephen C Nov 07 '18 at 13:34
  • But looking at the code in your comment .... it looks like you are double encoding the password. Is that intended? – Stephen C Nov 08 '18 at 02:37