我有关于创建httpurlconnection(或httpsurlconnection)的问题。实际上这是关于在转换url.openconnection之后设置连接头。在运行相同的代码之前没有运行。我无法理解。 (例外:已连接......或类似..)
有时会运行直到 httpConn.getOutPutStream(); 此时它会出现错误“java.net.ProtocolException:OutputStream不可用,因为请求标头已经发送!”
我的错在哪里?
在我使用代码之前:所有代码都是here(我还有关于This Code的另一个问题)
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"));
答案 0 :(得分:1)
这是对的!
“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"));