javascript - 此代码返回 301 永久移动,而同一脚本正常工作

标签 javascript java http http-status-code-301

这是 java 脚本的一部分,用于从网站 ( https://web.expasy.org/protparam/ ) 收集 .xls 文件中的一些数据。该网站有一个框,我们在其中输入一些字符,然后它会计算并重定向到另一个 URL ( https://web.expasy.org/cgi-bin/protparam/protparam )。整个java脚本工作正常,但有一段时间它不起作用,我尝试诊断问题,现在我在控制台上收到错误

""<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://web.expasy.org/cgi-bin/protparam/protparam">here</a>.</p>
</body></html>"

有人可以解决这个问题吗? 我必须声明一件事:我不是来自核心编程领域,因此我需要你的帮助。

//initializing the url 
URL siturl = new URL("http://web.expasy.org/cgi-bin/protparam/protparam");
//opening the siturl connection
HttpURLConnection conn = null;
conn = (HttpURLConnection)siturl.openConnection();
conn.setRequestMethod("POST");
conn.setFollowRedirects(true);
conn.setRequestProperty("Content-length", String.valueOf(data.length())); 
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoOutput(true);
conn.setDoInput(true);
//setting the output condition to true for printing the contents
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
// used to convert character to bytes
wr.write(data);
//System.out.println(data);
wr.flush();
wr.close();
// Get the response
BufferedWriter out = new BufferedWriter(new FileWriter("xlsresult.xls",true));
// printing the results to a text file
//out.write(data);            
out.write(profilename+ "\t");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;

最佳答案

您正在以 HTTP 方式打开连接,但页面现在是 HTTPS,因此会向您发送 HTTP 301(因为它现在位于 https://web.expasy.org/cgi-bin/protparam/protparam 而不是 http://web.expasy.org/cgi-bin/protparam/protparam )。至少这是我的猜测(我不经常使用 HTTP)。

改变

    URL siturl = new URL("http://web.expasy.org/cgi-bin/protparam/protparam");

    URL siturl = new URL("https://web.expasy.org/cgi-bin/protparam/protparam");

可能会成功。

关于javascript - 此代码返回 301 永久移动,而同一脚本正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61907942/

相关文章:

javascript - 子路由中的 routerLink 错误

javascript - 如何在 Angular 2 中使用交互式美国 map 或如何在 Angular2 中导入和使用外部 javascript

java - 我怎样才能将其更改为数组?

Java小程序: AccessControlException again

java - 如何定义一个好的存储库接口(interface)

docker - 用于通信 2 个 docker 容器的 ip 地址是什么?

file - HTTPS 协议(protocol)文件完整性

javascript - 为什么类型 'number' 不能分配给类型 'T'?

javascript - Meteor 模式和集合的多个文件

node.js - 在 Node.js 中验证 API 输入的最佳实践?