在编写访问URL的代码方面经验为零,下面是我尝试解决我的问题的尝试,请耐心等待。 为了解释这种情况-我正在尝试自动化从世界卫生组织网站下载zip文件的过程。该文件的路径如下:
尝试输入以下代码:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
public class WHODataDownload{
public static void main(String[] args) {
// Install Authenticator
MyAuthenticator.setPasswordAuthentication("username", "password");
Authenticator.setDefault (new MyAuthenticator ());
try {
String url = "https://login.microsoftonline.com/whoumcprod.onmicrosoft.com/oauth2/authorize?client_id=bb2a2e3a-c5e7-4f0a-88e0-8e01fd3fc1f4&redirect_uri=https%3a%2f%2flogin.microsoftonline.com%2fte%2fwhoumcprod.onmicrosoft.com%2foauth2%2fauthresp&response_type=id_token&scope=email+openid&response_mode=query&nonce=2rEvxJKKHe4qFrsr%2feJh4g%3d%3d&nux=1&nca=1&domain_hint=whoumcprod.onmicrosoft.com&mkt=en-US&lc=1033&state=StateProperties%3deyJTSUQiOiJ4LW1zLWNwaW0tcmM6YWNjMzk4YzctZTdiNy00MTM0LWFjNDktMjgyZDg4NDJiOTc1IiwiVElEIjoiMWMwYTNjOTgtODY1Ny00YTEzLTlmMWMtNjMyYTE3MDc4YjJjIn0";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
System.out.println("Request URL ... " + url);
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
URL newUrl = new URL ("https://www.who-umc.org/user-area/vigibase-extract-case-level/");
conn = (HttpURLConnection) newUrl.openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Chrome");
conn.addRequestProperty("Referer", "google.com");
URL downloadUrl = new URL("https://downloadarea.who-umc.org/download/product?id=1254");
HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection();
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream("download.zip");
copy(in, out, 1024);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
byte[] buf = new byte[bufferSize];
int n = input.read(buf);
while (n >= 0) {
output.write(buf, 0, n);
n = input.read(buf);
}
output.flush();
}
}
这是验证码:
import java.net.Authenticator;
import java.net.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
private static String username = "";
private static String password = "";
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (MyAuthenticator.username,
MyAuthenticator.password.toCharArray());
}
public static void setPasswordAuthentication(String username, String password) {
MyAuthenticator.username = username;
MyAuthenticator.password = password;
}
}