简体   繁体   English

HttpURLConnection甚至在setDoOutput(true)之后执行get请求,setRequestMethod(“POST”)setRequestProperty(“Content”

[英]HttpURLConnection doing a get request even after setDoOutput(true), setRequestMethod(“POST”) setRequestProperty("Content

Here is the code: 这是代码:

String Surl = "http://mysite.com/somefile";
String charset = "UTF-8";
query = String.format("param1=%s&param2=%s",
URLEncoder.encode("param1", charset),
URLEncoder.encode("param2", charset));

HttpURLConnection urlConnection = (HttpURLConnection) new URL(Surl + "?" + query).openConnection();
urlConnection.setRequestMethod("POST");             
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setAllowUserInteraction(false);
urlConnection.setRequestProperty("Accept-Charset", charset);
urlConnection.setRequestProperty("User-Agent","<em>Android</em>");
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=" + charset);                
urlConnection.connect();

The above still does a GET request. 以上仍然是GET请求。 I am using PHP on the server and am able to access the query's 'name=value' params through the $_GET variable and not the $_POST variable 我在服务器上使用PHP,并且能够通过$_GET变量而不是$_POST变量访问查询的'name = value'参数
Tested on 2.3.7(device). 测试2.3.7(设备)。

What am I missing ? 我错过了什么?

When you send parameters in the url they are put in the GET variable. 当您在URL中发送参数时,它们将被放入GET变量中。 You should be posting the parameters in the POST body of the request to achieve what you are looking for. 您应该在请求的POST主体中发布参数以实现您要查找的内容。 You should add the following just before the connect() call and remove the "?" 您应该在connect()调用之前添加以下内容并删除“?” + query from the url. +来自网址的查询。

    urlConnection.setRequestProperty("Content-Length", String.valueOf(query.getBytes().length));            
    urlConnection.setFixedLengthStreamingMode(query.getBytes().length);

    OutputStream output = new BufferedOutputStream(urlConnection.getOutputStream());            
    output.write(query.getBytes());
    output.flush(); 
    output.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尽管setDoOutput(true)和setRequestMethod(“POST”),HttpURLConnection始终执行GET请求而不是POST请求 - HttpURLConnection performs always a GET request instead of a POST request in spite of setDoOutput(true) and setRequestMethod(“POST”) 即使httpCon.setRequestMethod(“GET”),HttpURLConnection也会发送POST请求;已设定 - HttpURLConnection sends a POST request even though httpCon.setRequestMethod(“GET”); is set HttpURLConnection,openConnection和setRequestMethod(“ GET” - HttpURLConnection, openConnection and setRequestMethod(“GET” Java .setRequestMethod()中的HTTP POST请求不执行任何操作 - HTTP POST Request in Java .setRequestMethod() Does Nothing Android HttpURLConnection setRequestMethod PUT - Android HttpURLConnection setRequestMethod PUT 当我尝试将setRequestProperty设置为HttpURLConnection时,Android会出错 - Android get an error when i try to setRequestProperty to HttpURLConnection 得到400响应httpurlconnection发送发布请求 - get 400 response httpurlconnection sends post request HttpURLConnection将我的POST请求重定向到GET - HttpURLConnection redirects my POST request into a GET HttpURLConnection 在发送 GET 请求后返回响应代码 500,即使它在本地工作 - HttpURLConnection returns response code 500 after sending a GET request even though it works locally 在Java中执行GET并添加标头请求后,无法进行POST - Failure making a POST after doing a GET in Java, and adding a header request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM