我关于 android 上的 HttpURLConnections 的问题。 (cast-URLConnection、url.openConnection、getOutPutStream()..)
我在创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是对的!
“httpConn.setInstanceFollowRedirects(true)”连接并向服务器发送标头。我关闭这段代码。
This is right!
"httpConn.setInstanceFollowRedirects(true)" connects and sends headers to server. I close this code.