如何在java中阅读HTML文件的内容 [英] How to read content of HTML file in java
问题描述
我正在尝试阅读html的电子邮件模板文件的内容。当我运行应用程序时它会抛出异常,因为
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...
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);
}
请帮我解决这个问题..
< br $>
谢谢
我的尝试:
Please help me to resolve this issue..
Thanks
What I have tried:
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
推荐答案
好吧,错误信息是不言自明的。网址无法访问...因此,您必须查看网址。
如果您确定网址是正确的,请尝试使用此解决方案:
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:
而不是在java中使用URLConnection,如果你使用HttpURLConnection,你应该可以从java访问所请求的网页。请尝试以下代码:
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:
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
使用urlConnection的普通java不会被接受访问互联网。要访问浏览器,它需要执行搜索而不需要HTTP响应代码:403 for URL
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
并根据您的需要进行更改。
来源: java.io. IOException:服务器返回HTTP响应代码:403为URL [ ^ ]
这篇关于如何在java中阅读HTML文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!