为什么服务器在 POST 方法请求后会返回 GET 响应?
我只是想发送一个请求并读出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以显式指定
POST
方法,如下所示(查看是否解决了问题):是的,
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):Yes,
URLConnection.seDoOutput(true)
means that you intend to use theURLConnection
for output (default, isfalse
) and implicitly tellsHttpURLConnection
to usePOST
.My assumption is that you're not passing request parameters to the Web Service. See this related SO post on using
URLConnection
.如果请求方法确实是 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.
简单的答案是:
在每个请求行的末尾添加一些“\n”,效果很好。
the simple answer is:
add some "\n" at the end of each request line, and it works fine.