关键词:
【中文标题】为啥使用 Java 将数据发布到网站时会出现 405 错误?【英文标题】:Why got 405 error when post data to website by using Java?为什么使用 Java 将数据发布到网站时会出现 405 错误? 【发布时间】:2012-05-15 15:22:03 【问题描述】:我只是尝试使用以下代码将数据发布到谷歌,但总是出现 405 错误,谁能告诉我方法?
package com.tom.labs;
import java.net.*;
import java.io.*;
public class JavaHttp
public static void main(String[] args) throws Exception
File data = new File("D:\\in.txt");
File result = new File("D:\\out.txt");
FileOutputStream out = new FileOutputStream(result);
OutputStreamWriter writer = new OutputStreamWriter(out);
Reader reader = new InputStreamReader(new FileInputStream(data));
postData(reader,new URL("http://google.com"),writer);//Not working
//postData(reader,new URL("http://google.com/search"),writer);//Not working
sendGetRequest("http://google.com/search", "q=Hello");//Works properly
public static String sendGetRequest(String endpoint,
String requestParameters)
String result = null;
if (endpoint.startsWith("http://"))
// Send a GET request to the servlet
try
// Send data
String urlStr = endpoint;
if (requestParameters != null && requestParameters.length() > 0)
urlStr += "?" + requestParameters;
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null)
sb.append(line);
rd.close();
result = sb.toString();
catch (Exception e)
e.printStackTrace();
System.out.println(result);
return result;
/**
* Reads data from the data reader and posts it to a server via POST
* request. data - The data you want to send endpoint - The server's address
* output - writes the server's response to output
*
* @throws Exception
*/
public static void postData(Reader data, URL endpoint, Writer output)
throws Exception
HttpURLConnection urlc = null;
try
urlc = (HttpURLConnection) endpoint.openConnection();
try
urlc.setRequestMethod("POST");
catch (ProtocolException e)
throw new Exception(
"Shouldn't happen: HttpURLConnection doesn't support POST??",
e);
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
OutputStream out = urlc.getOutputStream();
try
Writer writer = new OutputStreamWriter(out, "UTF-8");
pipe(data, writer);
writer.close();
catch (IOException e)
throw new Exception("IOException while posting data", e);
finally
if (out != null)
out.close();
InputStream in = urlc.getInputStream();
try
Reader reader = new InputStreamReader(in);
pipe(reader, output);
reader.close();
catch (IOException e)
throw new Exception("IOException while reading response", e);
finally
if (in != null)
in.close();
catch (IOException e)
e.printStackTrace();
throw new Exception("Connection error (is server running at "
+ endpoint + " ?): " + e);
finally
if (urlc != null)
urlc.disconnect();
/**
* Pipes everything from the reader to the writer via a buffer
*/
private static void pipe(Reader reader, Writer writer) throws IOException
char[] buf = new char[1024];
int read = 0;
while ((read = reader.read(buf)) >= 0)
writer.write(buf, 0, read);
writer.flush();
【问题讨论】:
如果谷歌不为他们的项目提供方便的界面 - 这意味着他们不希望机器人与他们互动,我敢打赌这违反了他们的 TOS PS:将所有自我描述和特定的例外转换为Excelption
绝不是一个好主意
谢谢,你能告诉我你是怎么处理我的代码的吗?为什么我不能正确格式化它?
如果您阅读有关格式化的文档,您可以对其进行格式化:***.com/editing-help
【参考方案1】:
405 表示“方法不允许”。例如,如果您尝试将 POST
发送到不允许 POST
的 URL,则服务器将返回 405 状态。
您想通过向 Google 发出 POST 请求来做什么?我怀疑 Google 的主页只允许 GET、HEAD 和 OPTIONS。
这是对 Google 的 POST 请求的正文,其中包含 Google 的解释。
405. 这是一个错误。
请求方法
POST
不适用于URL/
。这就是我们所知道的。
【讨论】:
汤姆,你为什么要发帖?你想达到什么目的?如果您想获取搜索结果,您应该使用 GET。 好的。我不应该使用 POST。但我发现我可以毫无错误地发帖到“yahoo.com”。 每个网站都不同。例如,Apache 似乎默认允许 POST,但它实际上并没有做任何事情。 如何允许/不允许get/post方法? @kanishk:“不允许”的全部意义在于你不能这样做。这就是“不允许”的意思。您唯一的选择是做其他允许的事情。为啥在使用等待组和通道时会出现死锁?
】为啥在使用等待组和通道时会出现死锁?【英文标题】:whyistheredeadlockwhenusingwaitgroupandchannelingo?为什么在使用等待组和通道时会出现死锁?【发布时间】:2022-01-1010:46:47【问题描述】:我想使用setter函数将0-9发送到ch1通道,然... 查看详情
为啥将按钮添加为子视图时会出现内存泄漏?
】为啥将按钮添加为子视图时会出现内存泄漏?【英文标题】:WhydoIgetamemoryleakwhenaddingabuttonasasubview?为什么将按钮添加为子视图时会出现内存泄漏?【发布时间】:2010-02-2219:04:12【问题描述】:我有一个使用tableview的应用程序,... 查看详情
为啥在更新现有实体时会出现重复的实体?
】为啥在更新现有实体时会出现重复的实体?【英文标题】:WhyamIgettingduplicateofentitieswhenIupdatinganexistingentity?为什么在更新现有实体时会出现重复的实体?【发布时间】:2015-01-2423:28:03【问题描述】:我将一些数据保存到我的数... 查看详情
为啥在使用 ssl 时会出现连接问题?
】为啥在使用ssl时会出现连接问题?【英文标题】:WhydoIgetaconnectionissuewhenusingssl?为什么在使用ssl时会出现连接问题?【发布时间】:2014-07-2418:01:12【问题描述】:我正在我的网站上运行一个聊天应用程序,当我使用HTTP时一切正... 查看详情
为啥在加载到 XMLTABLE 时会跳过 XML 文档的第 40,000 个字符?
】为啥在加载到XMLTABLE时会跳过XML文档的第40,000个字符?【英文标题】:Whyisthe40,000thcharofanXMLdocumentskippedwhenloadingtoanXMLTABLE?为什么在加载到XMLTABLE时会跳过XML文档的第40,000个字符?【发布时间】:2020-02-1215:51:42【问题描述】:将50... 查看详情
为啥在使用 kotlin 将数据添加到 firebase 实时数据库时会得到重复值?
】为啥在使用kotlin将数据添加到firebase实时数据库时会得到重复值?【英文标题】:Whydoigetduplicatevalueswhenaddingdatatofirebaserealtimedatabaseusingkotlin?为什么在使用kotlin将数据添加到firebase实时数据库时会得到重复值?【发布时间】:2021... 查看详情
为啥在将使用 Diesel 特征的函数重写为特征方法时会出现“溢出评估需求”?
】为啥在将使用Diesel特征的函数重写为特征方法时会出现“溢出评估需求”?【英文标题】:WhydoIget"overflowevaluatingtherequirement"whenrewritingafunctionusingDiesel\'straitsintoatraitmethod?为什么在将使用Diesel特征的函数重写为特征方法... 查看详情
为啥直接将 Arrays.asList() 分配给 var 时会出现 AssertionError?
】为啥直接将Arrays.asList()分配给var时会出现AssertionError?【英文标题】:WhyamIgettinganAssertionErrorwhenassigningArrays.asList()tovardirectly?为什么直接将Arrays.asList()分配给var时会出现AssertionError?【发布时间】:2018-09-0813:52:52【问题描述】:... 查看详情
为啥当我将整个网站上传到虚拟主机时会扩展?
】为啥当我将整个网站上传到虚拟主机时会扩展?【英文标题】:WhydoesmyentiresitescalewhenIuploadittoawebhost?为什么当我将整个网站上传到虚拟主机时会扩展?【发布时间】:2012-10-0803:45:35【问题描述】:在我的本地计算机上测试我的... 查看详情
为啥在使用 WriteValue 更新布尔绑定时会出现格式异常?
】为啥在使用WriteValue更新布尔绑定时会出现格式异常?【英文标题】:WhywouldIgetaformatexceptionwhenupdatingabooleanbindingwithWriteValue?为什么在使用WriteValue更新布尔绑定时会出现格式异常?【发布时间】:2015-09-0516:17:58【问题描述】:我... 查看详情
为啥我在使用 tkinter 时会出现这个 TypeError?
】为啥我在使用tkinter时会出现这个TypeError?【英文标题】:WhydoIhavethisTypeErrorwhenusingtkinter?为什么我在使用tkinter时会出现这个TypeError?【发布时间】:2010-11-2705:48:20【问题描述】:所以我从2.6升级到python3.1.1并运行了我的一个使... 查看详情
为啥我将邮件内容发送到 Hotmail 帐户时会作为附件发送?
】为啥我将邮件内容发送到Hotmail帐户时会作为附件发送?【英文标题】:whymailcontentissentasattachmentwhenIsendittoHotmailaccount?为什么我将邮件内容发送到Hotmail帐户时会作为附件发送?【发布时间】:2011-03-2314:20:40【问题描述】:为什... 查看详情
为啥将 NSManagedObjectContext 保存为“干净”时会出现合并错误?
】为啥将NSManagedObjectContext保存为“干净”时会出现合并错误?【英文标题】:WhyamIgettingamergeerrorwhensavinganNSManagedObjectContextthis\'clean\'?为什么将NSManagedObjectContext保存为“干净”时会出现合并错误?【发布时间】:2011-01-2714:14:55【... 查看详情
为啥 firebug 在调试时会改变网站的行为?
】为啥firebug在调试时会改变网站的行为?【英文标题】:Whydoesfirebugchangethebehaviorofawebsitewhendebugging?为什么firebug在调试时会改变网站的行为?【发布时间】:2012-04-0711:17:49【问题描述】:我正在开发一个使用Extjs的网站,我正在... 查看详情
为啥在链接到库时会出现错误,但在构建时却没有?
】为啥在链接到库时会出现错误,但在构建时却没有?【英文标题】:WhydoIgeterrorswhenIlinktoalibrary,butnotwhenIbuildit?为什么在链接到库时会出现错误,但在构建时却没有?【发布时间】:2015-02-0912:32:27【问题描述】:我正在为c++库(IT... 查看详情
为啥将 ViewModel 传递给 View 时会出现此错误?
】为啥将ViewModel传递给View时会出现此错误?【英文标题】:WhydoIgetthiserrorwhenpassingViewModeltoView?为什么将ViewModel传递给View时会出现此错误?【发布时间】:2021-06-0909:43:34【问题描述】:将ViewModel传递给View时出现错误传入ViewDataDict... 查看详情
为啥我在使用 selenium/webkit 和 capybara 时会出现空白页?
】为啥我在使用selenium/webkit和capybara时会出现空白页?【英文标题】:WhyIgetblankpageswhenusingselenium/webkitwithcapybara?为什么我在使用selenium/webkit和capybara时会出现空白页?【发布时间】:2012-08-2907:41:43【问题描述】:我正在尝试使用... 查看详情
为啥使用 SingleAsync 时会出现此错误?
】为啥使用SingleAsync时会出现此错误?【英文标题】:WhydoesthiserroroccurwhenusingSingleAsync?为什么使用SingleAsync时会出现此错误?【发布时间】:2019-11-0312:19:36【问题描述】:查找项目是否重复。预期:仅在找到多个项目时才抛出异... 查看详情