Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpURLConnection始终执行GET请求而不是POST请求,不管setDoOutput(true)和setRequestMethod(“POST”)如何_Java_Android_Http_Android 4.0 Ice Cream Sandwich - Fatal编程技术网

Java HttpURLConnection始终执行GET请求而不是POST请求,不管setDoOutput(true)和setRequestMethod(“POST”)如何

Java HttpURLConnection始终执行GET请求而不是POST请求,不管setDoOutput(true)和setRequestMethod(“POST”)如何,java,android,http,android-4.0-ice-cream-sandwich,Java,Android,Http,Android 4.0 Ice Cream Sandwich,由于更新了冰淇淋三明治,我的发帖请求不再有效。在ICS之前,这可以正常工作: try { final URL url = new URL("http://example.com/api/user"); final HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("

由于更新了冰淇淋三明治,我的发帖请求不再有效。在ICS之前,这可以正常工作:

try {
        final URL url = new URL("http://example.com/api/user");
        final HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(false);
        connection.setDoInput(true);
        connection.setRequestProperty("Content-Length", "0");
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Log.w(RestUploader.class.getSimpleName(), ": response code: " + connection.getResponseMessage());
        } else {
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
            final String line = reader.readLine();
            reader.close();
            return Long.parseLong(line);
        }
    } catch (final MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return -1;
我已经试着设置了

connection.setDoOutput(true);
但它不起作用。服务器响应始终是405(不允许使用方法),服务器日志表明这是一个GET请求

Android JavaDoc to setRequestMethod表示:

只能在建立连接之前调用此方法

这是否意味着必须在url.openConnection()之前调用该方法?我应该如何创建HttpURLConnection实例?我所看到的所有例子都是如上所述的

我希望有人知道为什么它总是发送GET请求而不是POST

提前谢谢

connection.setRequestMethod("POST");
connection.setDoOutput(false);
在上面的两个陈述中,这是正确的

connection.setDoOutput(true)
以前

connection.setRequestMethod("POST")

语句

My.htaccess具有重写规则,该规则将www重定向到http://。结果证明我的Java代码工作得很好!重定向/重写规则使我的第一个请求(POST)转发到http,因此“成为”GET。我的PHP脚本只听POST,我挠了挠脑袋,直到我发现自己犯了重定向规则的错误。检查一下你的“问题”

对我来说,先设置setDoOutput或setRequestMethod并不重要,任何顺序都可以(API 23)。目前情况是这样的:

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setInstanceFollowRedirects(false);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type",bla bla
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setRequestProperty("Content-Length", String.valueOf(PARAMETER_VARIABLE.getBytes().length));
con.setFixedLengthStreamingMode(PARAMETER_VARIABLE.getBytes().length);
OutputStream os = con.getOutputStream();
os.write(PARAMETER_VARIABLE.getBytes());
os.flush();
os.close();

您是否在UI线程中使用HttpRequest?否,我在
new AsyncTask(){@Override protected Boolean doInBackground(final Void…params){/…}中调用该方法
您的答案可以在这个问题中找到:@Kekoa OP正在尝试一篇文章,而不是一篇文章。这个问题尚未发布。您找到了这个问题的解决方案吗?我现在也遇到了同样的问题。