How can I prevent a **"java.net.ProtocolException: Cannot write output after reading input"** error code in Java?

huangapple 未分类评论47阅读模式
英文:

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();

huangapple
  • 本文由 发表于 2020年5月4日 14:40:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/61586499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定