我关于 android 上的 HttpURLConnections 的问题。 (cast-URLConnection、url.openConnection、getOutPutStream()..)

发布于 2024-12-10 13:39:43 字数 1759 浏览 0 评论 0原文

我在创建 httpurlconnection(或 httpsurlconnection)时遇到问题。实际上,这是关于在强制转换 url.openconnection 后设置连接标头。在运行之前,相同的代码现在未运行。我无法理解。 (例外:已经连接...或类似..)

有时它会运行直到httpConn.getOutPutStream();此时它会给出错误“java.net.ProtocolException:OutputStream不可用,因为请求标头有已经发送了!”

我的错在哪里??

在我使用代码之前:所有代码都位于此处(我还有一个关于此代码,也是)

URL url = new URL(getUrl());
URLConnection conn = url.openConnection();
HttpURLConnection httpConn=(HttpURLConnection)conn; /*or HttpsURLConnection*/
httpConn.set...
...
httpConn.connect();
...

我尝试设置一些投射后的设置,但结果是一样的。 conn.set.. 正在工作,但在投射后我无法设置任何内容...

更改了代码:

        URL url = new URL(getUrl());
        conn = url.openConnection();
        conn.setAllowUserInteraction(false);
        conn.setConnectTimeout(10000);
        conn.setRequestProperty("Accept-Charset", "utf-8");
        conn.setRequestProperty("Content-Type",
                "text/xml; charset=utf-8");
        conn.setRequestProperty("SOAPAction",
                "http://tempuri.org/IAuthenticationServiceNew/Authenticate");
        conn.setRequestProperty("Software-Version", AppData.VERSION);
        conn.setDoOutput(true);
        httpConn = (HttpsURLConnection) conn;
        httpConn.setChunkedStreamingMode(getParams().getBytes("UTF8").length);
        httpConn.setInstanceFollowRedirects(true);

        httpConn.connect();
        os = httpConn.getOutputStream();
        os.write(getParams().getBytes("UTF8"));

I have a problem about creating a httpurlconnection(or httpsurlconnection). Actually this is about setting connection headers after casting url.openconnection.Before the running same code is not running now. I couldn't understand. (Exception : already connected... or like.. )

Some times it runs until httpConn.getOutPutStream(); At this time it gives error "java.net.ProtocolException: OutputStream unavailable because request headers have already been sent!"

Where is my fault??

Before I used code: All code is here (I have another questıon about This Code, too)

URL url = new URL(getUrl());
URLConnection conn = url.openConnection();
HttpURLConnection httpConn=(HttpURLConnection)conn; /*or HttpsURLConnection*/
httpConn.set...
...
httpConn.connect();
...

I try to set some settings after casting , but result is same. conn.set..'s are working, but after casting I can't set anything...

Changed Code:

        URL url = new URL(getUrl());
        conn = url.openConnection();
        conn.setAllowUserInteraction(false);
        conn.setConnectTimeout(10000);
        conn.setRequestProperty("Accept-Charset", "utf-8");
        conn.setRequestProperty("Content-Type",
                "text/xml; charset=utf-8");
        conn.setRequestProperty("SOAPAction",
                "http://tempuri.org/IAuthenticationServiceNew/Authenticate");
        conn.setRequestProperty("Software-Version", AppData.VERSION);
        conn.setDoOutput(true);
        httpConn = (HttpsURLConnection) conn;
        httpConn.setChunkedStreamingMode(getParams().getBytes("UTF8").length);
        httpConn.setInstanceFollowRedirects(true);

        httpConn.connect();
        os = httpConn.getOutputStream();
        os.write(getParams().getBytes("UTF8"));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

递刀给你 2024-12-17 13:39:43

这是对的!
“httpConn.setInstanceFollowRedirects(true)”连接并向服务器发送标头。我关闭这段代码。

        URL url = new URL(getUrl());
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setDoInput(true);
        httpConn.setDoOutput(true);
        httpConn.setAllowUserInteraction(false);
        httpConn.setUseCaches(false);
        httpConn.setConnectTimeout(10000);
        httpConn.setRequestProperty("Accept-Charset", "utf-8");
        httpConn.setRequestProperty("Content-Type",
                "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction",
                "http://tempuri.org/IAuthenticationServiceNew/"
                        + conTypeString);
        httpConn.setRequestProperty("Software-Version", AppData.VERSION);
        httpConn.setRequestMethod("POST");
        httpConn.setChunkedStreamingMode(0);
        os = httpConn.getOutputStream();
        os.write(getParams().getBytes("UTF8"));

This is right!
"httpConn.setInstanceFollowRedirects(true)" connects and sends headers to server. I close this code.

        URL url = new URL(getUrl());
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setDoInput(true);
        httpConn.setDoOutput(true);
        httpConn.setAllowUserInteraction(false);
        httpConn.setUseCaches(false);
        httpConn.setConnectTimeout(10000);
        httpConn.setRequestProperty("Accept-Charset", "utf-8");
        httpConn.setRequestProperty("Content-Type",
                "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction",
                "http://tempuri.org/IAuthenticationServiceNew/"
                        + conTypeString);
        httpConn.setRequestProperty("Software-Version", AppData.VERSION);
        httpConn.setRequestMethod("POST");
        httpConn.setChunkedStreamingMode(0);
        os = httpConn.getOutputStream();
        os.write(getParams().getBytes("UTF8"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文