我在创建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"));
最佳答案
这是正确的!
“ 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"));