错误:读取输入后无法写入输出

时间:2012-12-29 14:18:48

标签: java inputstream bufferedreader

我想从java桌面应用程序登录网站,但主要问题是textbox ID 会随着每个请求而变化。

<input type="text" maxlength="10" name="UserName_88515" id="UserName_88515" />

这里id保持不变直到_,接下来的5位数随每个请求而变化。所以我决定首先阅读网页并检索5位数。然后我尝试写入数据登录网站。但是我收到了上面提到的错误。请帮忙。

public class LoginHandler {

static boolean isLoggedIn = false;
static String toastText, myText;

public void login(String usrname, String password, String cookys, String sessionCode) {

        try {

            URL url = new URL("http://www.somewebsite.com/home.php?session="
                    + sessionCode);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0");
            conn.setRequestProperty("Cookie", cookys);
            conn.setDoOutput(true);

            final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            final StringBuilder response = new StringBuilder();
            myText = response.toString();
            String line;

            while ((line = rd.readLine()) != null) {
                response.append(line);
            }
            rd.close();

            final String text1 = response.toString();
            final int starIndex = text1.indexOf("UserName_");
            final int endIndex = starIndex + 15;
            System.err.println("This is starIndex" + starIndex);
            System.err.println("This is endIndex" + endIndex);
            final String avc = text1.substring(starIndex, endIndex);
            System.err.println("This is avc\n" + avc);
            final String fin = avc.substring(10, 15);
            System.err.println("This is fin\n" + fin);

            final String data1 = URLEncoder.encode("MessageLength", "UTF-8")
                    + "=" + URLEncoder.encode("140", "UTF-8") + "&"
                    + URLEncoder.encode("UserName_" + fin, "UTF-8") + "="
                    + URLEncoder.encode("username", "UTF-8") + "&"
                    + URLEncoder.encode("Password_" + fin, "UTF-8") + "="
                    + URLEncoder.encode("password", "UTF-8") + "&"
                    + URLEncoder.encode("LoginNowbtnDiv", "UTF-8") + "="
                    + URLEncoder.encode("Login Now", "UTF-8") + "&"
                    + URLEncoder.encode("LoginNow", "UTF-8") + "="
                    + URLEncoder.encode("Login Now", "UTF-8");

            System.err.println("THIS IS Data1:\n " + data1);

            final OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data1);
            wr.flush();

            final BufferedReader rd1 = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            final StringBuilder response1 = new StringBuilder();
            String line1;

            while ((line1 = rd1.readLine()) != null) {
                response1.append(line1);
            }

            final String text2 = response1.toString();
            System.err.println("This is second response\n" + text2);
            rd1.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

修改

SEVERE: java.net.ProtocolException: Cannot write output after reading input.
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1083)
at mypack.UltooSender.send(UltooSender.java:80)
at mypack.NewServlet.processRequest(NewServlet.java:41)
at mypack.NewServlet.doGet(NewServlet.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)

SEVERE:     at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1083)
SEVERE:     at mypack.UltooSender.send(UltooSender.java:80)
SEVERE:     at mypack.NewServlet.processRequest(NewServlet.java:41)
SEVERE:     at mypack.NewServlet.doGet(NewServlet.java:63)
SEVERE:     at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
SEVERE:     at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
SEVERE:     at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
SEVERE:     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
SEVERE:     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
SEVERE:     at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
SEVERE:     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
SEVERE:     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
SEVERE:     at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
SEVERE:     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
SEVERE:     at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE:     at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE:     at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE:     at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE:     at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE:     at java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:3)

如果您在获取HttpURLConnection.getInputStream()后尝试获取HttpURLConnection.getOutputStream(),则会发生此异常。

在这一行中,您将获得InputStream,

final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

在这一行中你试图得到outputStream,

final OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());