开发者

Why does Server response with GET after POST method request?

开发者 https://www.devze.com 2023-04-10 21:16 出处:网络
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();

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://sche开发者_Python百科mas.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.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号