![](/img/trans.png)
[英]Why is it OkHttp post method sends get request instead of post request?
[英]Why does Server response with GET after POST method request?
我只是想發送一個請求並讀出一個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請求為null”所以看起來服務器通過GET方法讀取請求。
我在網絡方面不是那么堅定,但據我setDoOutput(true);
,我通過setDoOutput(true);
確保setDoOutput(true);
使用POST方法,不是嗎?
當我得到任何答案時,我知道有一個聯系,但可能是頭部的東西是錯的? 在每種情況下是否有必要發送它?
所以我的問題是什么原因可能是什么讓我或服務器對http方法感到困惑?
我想我錯過了java網絡處理。
對每一個幫助都很高興。
您還可以明確地指定POST
方法,如下所示(以查看是否解決了問題):
connection.setRequestMethod("POST");
connection.connect();
是的, URLConnection.seDoOutput(true)
表示您打算使用URLConnection
進行輸出(默認值為false
),並隱式告訴HttpURLConnection
使用POST
。
我的假設是你沒有將請求參數傳遞給Web服務。 使用URLConnection
查看此相關的SO帖子 。
如果請求方法確實是GET並且它在HTTP級別是非法的,那么根本不會獲得所有XML,只是一個HTTP錯誤代碼。 看起來更像是目標上的SOAP層問題,或者是您要發送的XML的問題。
簡單的答案是:在每個請求行的末尾添加一些“\\ n”,它工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.