1

我正在做客户端服务器通信。我通过URLConnection课程得到了联系。

现在我正在尝试将登录信息发送到服务器,服务器将检查信息是否正确,否则它将要求我再次登录,对于这种情况,我们假设登录不成功。但是当我再次尝试发送登录信息时得到服务器的响应后,我得到了

java.net.ProtocolException: Cannot write output after reading input.

这是我的代码:

URL url = new URL(uniRL);

java.net.URLConnection connection = url.openConnection();
connection.setAllowUserInteraction(true);
connection.setDoOutput(true);

while(true){
    System.out.println("Enter 1-login , 2-Exit");
useroption = input.nextLine();
numOption = Integer.parseInt(useroption);
if( numOption == 1){
    OutputStreamWriter writer = new OutputStreamWriter(
            connection.getOutputStream());
    user_login = login();
    writer.write(user_login[0]+"@");
    writer.write(user_login[1]);
    writer.flush();
    //out.close();
    BufferedReader in = new BufferedReader(
            new InputStreamReader(
                    connection.getInputStream()));
    while ((tempString = in.readLine()) != null) {
        decodedString = tempString;
        //System.out.println(decodedString);
        //System.out.println(decodedString.equalsIgnoreCase("unknown user"));
    }
    in.close();
    if((decodedString.equalsIgnoreCase("unknown user"))){continue;}
    else{break;}
}
4

1 回答 1

0

When you call in.close(), you're actually closing the stream used by the URL connection. You'd need to call url.openConnection() again to re-open the stream...

于 2012-09-19T10:10:41.353 回答