In my current project, I had a requirement to read the WSDL file from the URL and store it into the database as CLOB.
在当前项目中,我需要从URL读取WSDL文件并将其作为CLOB存储到数据库中。
There was no validation required, so it was kind of reading URL content to String and then storing it into the database table.
不需要验证,因此它是将URL内容读取到String,然后将其存储到数据库表中。
Java读取URL到字符串 (Java Read URL to String)
Here is the program I wrote in Java to read URL to String.
这是我用Java编写的用于读取URL到String的程序。
package com.journaldev.java;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class ReadURLToString {
public static void main(String[] args) throws Exception {
URL test = new URL("https://journaldev.com");
URLConnection uc = test.openConnection();
uc.addRequestProperty("User-Agent", "Mozilla/4.0");
BufferedReader in = new BufferedReader(new InputStreamReader(uc
.getInputStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
System.out.println(inputLine);
}
in.close();
System.out.println("HTML Data:" + sb.toString());
}
}
When we run the above program, it produces the following output.
当我们运行上述程序时,它将产生以下输出。
Most of the code is self-understood except setting the HTTP user agent.
除了设置HTTP用户代理外,大多数代码都是易于理解的。
For some websites, if you don’t set User-Agent
header, you might get 403 error code. It’s because they have web server security in place to avoid bot traffic.
对于某些网站,如果未设置User-Agent
标头,则可能会收到403错误代码。 这是因为它们具有适当的Web服务器安全性,可避免漫游器流量。
If you remove the setting of User-Agent from the above program, it will produce the following error.
如果从上述程序中删除User-Agent的设置,将产生以下错误。
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.journaldev.com/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at ReadURLToString.main(ReadURLToString.java:12)
If you have landed here and looked for something similar, feel free to use the above code. Don’t forget to comment or share with others too. That’s all for reading URL content in java program.
如果您登陆这里并寻找类似的东西,请随意使用上面的代码。 不要忘记发表评论或与他人分享。 这就是在Java程序中读取URL内容的全部。
Reference: Java URLConnection API Doc