桌面客户端Web服务器通信

时间:2012-09-19 10:06:34

标签: java servlets webserver client-server protocolexception

我在做客户端服务器通信。我通过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;}
}

1 个答案:

答案 0 :(得分:0)

当您致电in.close()时,您实际上正在关闭网址连接所使用的流。您需要再次致电url.openConnection()以重新打开流...