Click here to Skip to main content
15,066,928 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to read the content of email template file which is of html.when i run the application it is throwing the exception as

java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8080/Core/sr/main/java/com/service/mailutil/impl/EmailContent.html


I have tried with following code...

Java
URLConnection connection = new URL("http://localhost:8080/Core/sr/main/java/com/service/mailutil/impl/EmailContent.html").openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
        connection.connect();

        BufferedReader r  = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            sb.append(line);
        }


Please help me to resolve this issue..

Thanks

What I have tried:

Java
URLConnection connection = new URL("http://localhost:8080/Core/sr/main/java/com/service/mailutil/impl/EmailContent.html").openConnection();
			connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
			connection.connect();

			BufferedReader r  = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

			StringBuilder sb = new StringBuilder();
			String line;
			while ((line = r.readLine()) != null) {
			    sb.append(line);
			}


when i directly run the url in the browser it is showing following message..on the page.

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Apr 30 14:28:56 GST 2018
There was an unexpected error (type=Forbidden, status=403).
Access Denied
Posted
Updated 1-May-18 10:03am
v2
Comments
Richard MacCutchan 30-Apr-18 7:42am
   
The error message is telling you everything you need to know. The server will not accept your request. You need to check your webserver logs to find out why.

1 solution

Well, an error message is self-explanatory. An url is not accessible... So, you have to check out url address.

If you're sure that url address is correct, try to use this solution:
Quote:
nstead of using URLConnection in java, if you use HttpURLConnection you should beable to access the requested web page from java. Try the following code:

Java
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); 
 httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");


Normal java using urlConnection wont be accepted to access the internet. To access the browser it will need to perform a search without theexception HTTP response code : 403 for URL

and change to your needs.

Source: java.io.IOException: Server returned HTTP response code: 403 for URL[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900