英文:
How can I prevent a **"java.net.ProtocolException: Cannot write output after reading input"** error code in Java?
问题
我尝试向以下网址发送数据:http://mahavidyalay.in/AcademicDevelopment/ServerDemo/Led10.php?status=1,但是它显示了错误:
> java.net.ProtocolException: 在读取输入后无法写入输出。
我认为错误是因为这行代码:`int responseCode = connection.getResponseCode();`,我不知道如何解决这个问题。
URL Transmeter_Url = new URL(data);
HttpURLConnection connection = (HttpURLConnection) Transmeter_Url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", Contetn_Type);
connection.setRequestProperty("Contetn-Length", Integer.toString(Post_Value.length()));
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-
US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
int responseCode = connection.getResponseCode();
System.out.println("获取响应代码:" + responseCode);
System.out.println("达到此处 7");
OutputStream outputStream = connection.getOutputStream();
System.out.println("达到此处 7");
outputStream.write(Post_Value.getBytes());
outputStream.flush();
outputStream.close();
英文:
I was try to send data to the url http://mahavidyalay.in/AcademicDevelopment/ServerDemo/Led10.php?status=1 but it showing me error:
> java.net.ProtocolException: Cannot write output after reading input.
I think error is occurring because of line int responseCode = connection.getResponseCode();
And I don't
know how to solve this.
URL Transmeter_Url = new URL(data);
HttpURLConnection connection = (HttpURLConnection) Transmeter_Url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type",Contetn_Type);
connection.setRequestProperty("Contetn-Length", Integer.toString(Post_Value.length()));
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-
US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
int responseCode = connection.getResponseCode();
System.out.println("Get Response code:"+responseCode);
System.out.println("Reached here 7");
OutputStream outputStream = connection.getOutputStream();
System.out.println("Reached here 7");
outputStream.write(Post_Value.getBytes());
outputStream.flush();
outputStream.close();
专注分享java语言的经验与见解,让所有开发者获益!
评论