https post fails while http post succeeds with almost same code snippet in java? -


there's server supports both http , https service. want set access password via java. problem met managed http failed https. use same code except different protocols. below code setting password:

    //url = new url("http://172.20.1.80/password");     //httpurlconnection connection = sethttpconnect((httpurlconnection) url.openconnection());      url = new url("https://172.20.1.80/password");     httpsurlconnection connection = sethttpsconnect((httpsurlconnection) url.openconnection());      sc = sslcontext.getinstance("tls");     sc.init(null, new trustmanager[] { new mytrust() }, new java.security.securerandom());     connection.setsslsocketfactory(sc.getsocketfactory());      connection.setdoinput(true);     connection.setdooutput(true);     stringbuffer set = new stringbuffer();     newpw = "123456";      boundary = "----abc123abc1234-java";     set.append(boundary + "\r\n").append("content-disposition: form-data; name=\"pw\"\r\n\r\n")             .append(newpw).append("\r\n" + boundary + "--\r\n")             .append("content-disposition: form-data; name=\"con_pw\"\r\n\r\n").append(newpw)             .append("\r\n" + boundary + "--\r\n");     out = new printwriter(connection.getoutputstream());     out.write(set.tostring());     out.flush();      in = new bufferedreader(new inputstreamreader(connection.getinputstream(), "utf-8"));     while ((str = in.readline()) != null) {         result += (str + "\n");         if (str.indexof(pwset) != -1) {             succ += 1;             setpwsucc = true;         }     } 

this connection attributes:

private static httpsurlconnection sethttpsconnect(httpsurlconnection c) {     c.setrequestproperty("user-agent", "java");     c.setrequestproperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");     c.setrequestproperty("accept-language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");     c.setrequestproperty("accept-encoding", "gzip, deflate");     c.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary);     c.setconnecttimeout(timeout); // timeout     c.setreadtimeout(timeout);     return c; } private static httpurlconnection sethttpconnect(httpurlconnection c) {     c.setrequestproperty("user-agent", "sxf");     c.setrequestproperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");     c.setrequestproperty("accept-language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");     c.setrequestproperty("accept-encoding", "gzip, deflate");     c.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary);     c.setconnecttimeout(timeout); // timeout     c.setreadtimeout(timeout);     return (httpurlconnection) c; } 

setting x509:

public class mytrust implements x509trustmanager{     public void checkclienttrusted(x509certificate[] arg0, string arg1) throws certificateexception {}     public void checkservertrusted(x509certificate[] arg0, string arg1) throws certificateexception {}     public x509certificate[] getacceptedissuers() {return null; }    } 

the server can print logs webpage, , here log seperately:

//log when https in use: length of rcvbuf: 336 rcvbuf:  post /password http/1.1 user-agent: java accept: text/html,application/xhtml+xml,app  lication/xml;q=0.9,*/*;q=0.8 accept-language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 accept-encoding  : gzip, deflate content-type: multipart/form-data; boundary=----abc123abc1234-java host: 172.20.1  .80 connection: keep-alive content-length: 203   //log when http in use: length of rcvbuf: 520 rcvbuf:  post /password http/1.1 user-agent: sxf accept: text/html,application/xhtml+xml,appl  ication/xml;q=0.9,*/*;q=0.8 accept-language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 accept-encoding:  gzip, deflate content-type: multipart/form-data; boundary=----abc123abc1234-java host: 172.20.1.  80 connection: keep-alive content-length: 203 ----abc123abc1234-java content-disposition:  m-data; name="pd" 123456 ----abc123abc1234-java-- content-disposition: form-data; n  ame="conpw" 123456 ----abc123abc1234-java--  

it seems when https in use, data not accepted server.

but 1 thing confuse me more: there's operation on server, , before executed, password checked. code correct password(the password set via chrome then), can execute operation. please comment if code helpful analysize problem.

can me make https work? 2nd question is, since nothing log printed in web page, 203 in content-length: 203? thank in advance!


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -