为什么服务器在 POST 方法请求后会返回 GET 响应?

发布于 2024-12-08 08:24:27 字数 1653 浏览 2 评论 0原文

我只是想发送一个请求并读出 ogc sos 服务器的答案。

发送请求:

connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset="+charset);
connection.connect();

读取响应:

output = connection.getOutputStream();
output.write(query.getBytes(charset));
input = new URL(url).openStream();
Reader reader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuilder response = new StringBuilder();           
String line = null;

while((line = bufferedReader.readLine()) != null)
   response.append(line+"\n");

bufferedReader.close();
output.close();

服务器的响应为:

<?xml version="1.0" encoding="UTF-8"?>
<ows:ExceptionReport xmlns:ows="http://www.opengis.net/ows/1.1"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0.0" 
   xsi:schemaLocation="http://schemas.opengis.net/ows/1.1.0/owsExceptionReport.xsd">
  <ows:Exception exceptionCode="InvalidRequest" locator="REQUEST">
  <ows:ExceptionText>The GET request null is not supported by this SOS.</ows:ExceptionText>
  </ows:Exception>
</ows:ExceptionReport>

格式是特殊的sos类型,但主要消息是“GET请求为空” 所以看起来服务器是通过 GET 方法读取请求的。

我对网络不是那么坚定,但据我了解,我通过 setDoOutput(true); 确保使用 POST 方法,不是吗?

当我得到任何答案时,我知道两者之间存在联系,但可能是头部出了问题吗?是不是每一种情况都需要发送?

所以我的问题是,是什么原因让我或服务器对 http 方法感到困惑?

我想我没有使用java网络处理。

会很高兴得到每一个帮助。

I'm just trying to send a request and read out the answer of an ogc sos server.

sent request:

connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset="+charset);
connection.connect();

read response:

output = connection.getOutputStream();
output.write(query.getBytes(charset));
input = new URL(url).openStream();
Reader reader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuilder response = new StringBuilder();           
String line = null;

while((line = bufferedReader.readLine()) != null)
   response.append(line+"\n");

bufferedReader.close();
output.close();

The response of the server is:

<?xml version="1.0" encoding="UTF-8"?>
<ows:ExceptionReport xmlns:ows="http://www.opengis.net/ows/1.1"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0.0" 
   xsi:schemaLocation="http://schemas.opengis.net/ows/1.1.0/owsExceptionReport.xsd">
  <ows:Exception exceptionCode="InvalidRequest" locator="REQUEST">
  <ows:ExceptionText>The GET request null is not supported by this SOS.</ows:ExceptionText>
  </ows:Exception>
</ows:ExceptionReport>

The format is a special sos type, but the main message is "The GET request null"
So it seem like the server read the request by GET method.

I am not that firm in networking, but as I understood, I ensure by setDoOutput(true); to use the POST method, isn't it?

As I get any answer, I know there is a connection, but may it be that something with the head is wrong? Is it necessary in every case to sent it?

So my question is just what could be the reason what makes me or the server confusing about the http methods?

I guess I am missusing the java network handling.

Would be glad about every help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山有枢 2024-12-15 08:24:27

您还可以显式指定 POST 方法,如下所示(查看是否解决了问题):

connection.setRequestMethod("POST");
connection.connect();

是的,URLConnection.seDoOutput(true) 意味着您打算使用 URLConnection 进行输出(默认为 false),并隐式告诉 HttpURLConnection 使用 POST

我的假设是您没有将请求参数传递给 Web 服务。请参阅此相关SO帖子 关于使用URLConnection

You can, also, explicitly, specify the POST method as follows (to see if that solved the problem):

connection.setRequestMethod("POST");
connection.connect();

Yes, URLConnection.seDoOutput(true) means that you intend to use the URLConnection for output (default, is false) and implicitly tells HttpURLConnection to use POST.

My assumption is that you're not passing request parameters to the Web Service. See this related SO post on using URLConnection.

凉墨 2024-12-15 08:24:27

如果请求方法确实是 GET 并且它在 HTTP 级别上是非法的,那么您根本不会得到所有 XML,而只会得到一个 HTTP 错误代码。看起来更像是目标处的 SOAP 层问题,或者您发送的 XML 的问题。

If the request method really was GET and it was illegal at the HTTP level you wouldn't get all that XML back at all, just an HTTP error code. Looks more like a SOAP layer problem at the target, or a problem with the XML you are sending.

2024-12-15 08:24:27

简单的答案是:
在每个请求行的末尾添加一些“\n”,效果很好。

the simple answer is:
add some "\n" at the end of each request line, and it works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文