i illegal state exception when set dooutput true.
public boolean sendlinkstomaster(string ipport, list<string> links){ boolean sent = false; string[] tokens = ipport.split(":"); string data = edu.cis555.searchengine.utils.utils.generatelinks(links); httpurlconnection conn=null; try{ string encodeddata = urlencoder.encode(data, "utf-8"); try{ string ip =tokens[0]; string port = tokens[1]; string path = edu.cis555.searchengine.utils.constants.url_add_link; system.setproperty("http.keepalive", "false"); url u = new url("http", ip, integer.parseint(port),"/"+path); conn = (httpurlconnection)u.openconnection(); //error in line conn.setdooutput(true); conn.setrequestmethod("post"); outputstream stream = conn.getoutputstream(); stream.write(encodeddata.getbytes()); stream.close(); if(conn.getresponsecode() == httpurlconnection.http_ok) sent=true; // log.debug(conn.getresponsecode()); conn.disconnect(); }catch(malformedurlexception mfe){ log.debug(mfe.getmessage()); if(conn!=null){ conn.disconnect(); } }catch(ioexception ioe){ log.debug(ioe.getmessage()); if(conn!=null){ conn.disconnect(); } } }catch(exception e){ log.debug(e.getmessage()); if(conn!=null){ conn.disconnect(); } } return sent; }
the stack trace displayed same is:
java.lang.illegalstateexception: connected @ java.net.urlconnection.setdooutput(unknown source) @ edu.upenn.cis455.xpathengine.utils.pool.threadpool.sendlinkstomaster(threadpool.java:357) @ edu.upenn.cis455.xpathengine.utils.pool.threadpool$worker.processandaddtoqueue(threadpool.java:314) @ edu.upenn.cis455.xpathengine.utils.pool.threadpool$worker.run(threadpool.java:269) @ java.lang.thread.run(unknown source)
i don't see doing wrong sending request. point out missing or doing wrong
i got same problem , solved it. in case, caused because had forgotten watch connection.getresponsecode()
in debugging interface in netbeans. hope might others making same mistake.
if have watch relative response value of request, such getresponsecode()
, getresponsemessage()
, getinputstream()
or connect()
, error in debugging mode.
all of previous methods implicitly call connect()
, fire request. when reach setdooutput
, connection made.