问题描述
大家好,最近有一個作業是要用httpclient來登入露天,程式到client.executeMethod(authget);就會出錯並出現Exception:Theservermember.ruten.com.twfailedtorespondwithavalidHTTPresponse查過封包發現,SSL到ApplicationData後就會出現Encryptedalertmessage,TCP就會斷開,目前都沒有想法,去估狗爬文,都只找到一些類似的解法,要請各位高手幫忙,感謝~(以下的code用於facebook可ApplicationData正常,不會送出Encryptedalertmessage)附上code:main.javaimportjava.security.cert.CertPathValidatorException;importorg.apache.commons.httpclient.*;importorg.apache.commons.httpclient.cookie.CookiePolicy;importorg.apache.commons.httpclient.cookie.CookieSpec;importorg.apache.commons.httpclient.methods.*;importorg.apache.commons.httpclient.params.HttpMethodParams;importorg.apache.commons.httpclient.protocol.Protocol;publicclassLoginruten{staticfinalStringLOGON_SITE="member.ruten.com.tw";staticfinalintLOGON_PORT=443;publicLoginruten(){super();}publicstaticvoidmain(String[]args)throwsException{Protocolmyhttps=newProtocol("https",newMySecureProtocolSocketFactory(),43);Protocol.registerProtocol("https",myhttps);HttpClientclient=newHttpClient();client.getHostConfiguration().setHost(LOGON_SITE,LOGON_PORT,myhttps);System.setProperty("apache.commons.httpclient.cookiespec","COMPATIBILITY");GetMethodauthget=newGetMethod("/user/login.php");try{client.executeMethod(authget);}catch(Exceptionx){System.out.printlnFight;}Header[]head=authget.getResponseHeaders();Header[]foot=authget.getResponseFooters();System.out.println("Loginformget:"+authget.getStatusLine().toString());authget.releaseConnection();CookieSpeccookiespec=CookiePolicy.getDefaultSpec();Cookie[]initcookies=cookiespec.match(LOGON_SITE,LOGON_PORT,"/",false,client.getState().getCookies());System.out.println("Initialsetofcookies:");if(initcookies.length==0){System.out.println("None");}else{for(inti=0;i<initcookies.length;i++){System.out.println("-"+initcookies[i].toString());}}client.getState().addCookies(initcookies);PostMethodauthpost=newPostMethod("/login.php?login_attempt=1");//PrepareloginparametersNameValuePaircharset_test=newNameValuePair("charset_test","&f");NameValuePairversion=newNameValuePair("version","1.0");NameValuePairreturn_session=newNameValuePair("return_session","0");NameValuePairpass=newNameValuePair("pass","tiantian");NameValuePairemail=newNameValuePair("email","rlby@tom.com");NameValuePairpersistent=newNameValuePair("persistent","1");authpost.setRequestBody(newNameValuePair[]{charset_test,version,return_session,pass,email,persistent});client.executeMethod(authpost);System.out.print(authpost.getResponseBodyAsString());System.out.println("Loginformpost:"+authpost.getStatusLine().toString());authpost.releaseConnection();Cookie[]logoncookies=cookiespec.match(LOGON_SITE,LOGON_PORT,"/",false,client.getState().getCookies());System.out.println("Logoncookies:");if(logoncookies.length==0){System.out.println("None");}else{for(inti=0;i<logoncookies.length;i++){System.out.println("-"+logoncookies[i].toString());}}intstatuscode=authpost.getStatusCode();if((statuscode==HttpStatus.SC_MOVED_TEMPORARILY)||(statuscode==HttpStatus.SC_MOVED_PERMANENTLY)||(statuscode==HttpStatus.SC_SEE_OTHER)||(statuscode==HttpStatus.SC_TEMPORARY_REDIRECT)){Headerheader=authpost.getResponseHeader("location");if(header!=null){Stringnewuri=header.getValue();if((newuri==null)||(newuri.equals(""))){newuri="/";}System.out.println("Redirecttarget:"+newuri);GetMethodredirect=newGetMethod(newuri);client.executeMethod(redirect);System.out.println("Redirect:"+redirect.getStatusLine().toString());redirect.releaseConnection();}else{System.out.println("Invalidredirect");System.exit(1);}}else{System.out.println("error");System.exit(1);}}}2.MySecureProtocolSocketFactory.java:importjava.io.IOException;importjava.net.InetAddress;importjava.net.InetSocketAddress;importjava.net.Socket;importjava.net.SocketAddress;importjava.net.UnknownHostException;importjava.security.KeyManagementException;importjava.security.NoSuchAlgorithmException;importjava.security.cert.CertificateException;importjava.security.cert.X509Certificate;importjavax.net.SocketFactory;importjavax.net.ssl.SSLContext;importjavax.net.ssl.TrustManager;importjavax.net.ssl.X509TrustManager;importorg.apache.commons.httpclient.ConnectTimeoutException;importorg.apache.commons.httpclient.params.HttpConnectionParams;importorg.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;publicclassMySecureProtocolSocketFactoryimplementsSecureProtocolSocketFactory{static{System.out.println("SSL:");}privateSSLContextsslcontext=null;privateSSLContextcreateSSLContext(){SSLContextsslcontext=null;try{sslcontext=SSLContext.getInstance("SSL");sslcontext.init(null,newTrustManager[]{newTrustAnyTrustManager()},newjava.security.SecureRandom());}catch(NoSuchAlgorithmExceptione){e.printStackTrace();}catch(KeyManagementExceptione){e.printStackTrace();}returnsslcontext;}privateSSLContextgetSSLContext(){if(this.sslcontext==null){this.sslcontext=createSSLContext();}returnthis.sslcontext;}publicSocketcreateSocket(Socketsocket,Stringhost,intport,booleanautoClose)throwsIOException,UnknownHostException{returngetSSLContext().getSocketFactory().createSocket(socket,host,port,autoClose);}publicSocketcreateSocket(Stringhost,intport)throwsIOException,UnknownHostException{returngetSSLContext().getSocketFactory().createSocket(host,port);}publicSocketcreateSocket(Stringhost,intport,InetAddressclientHost,intclientPort)throwsIOException,UnknownHostException{returngetSSLContext().getSocketFactory().createSocket(host,port,clientHost,clientPort);}publicSocketcreateSocket(Stringhost,intport,InetAddresslocalAddress,intlocalPort,HttpConnectionParamsparams)throwsIOException,UnknownHostException,ConnectTimeoutException{if(params==null){thrownewIllegalArgumentException("Parametersmaynotbenull");}inttimeout=params.getConnectionTimeout();SocketFactorysocketfactory=getSSLContext().getSocketFactory();if(timeout==0){returnsocketfactory.createSocket(host,port,localAddress,localPort);}else{Socketsocket=socketfactory.createSocket();SocketAddresslocaladdr=newInetSocketAddress(localAddress,localPort);SocketAddressremoteaddr=newInetSocketAddress(host,port);socket.bind(localaddr);socket.connect(remoteaddr,timeout);returnsocket;}}privatestaticclassTrustAnyTrustManagerimplementsX509TrustManager{publicvoidcheckClientTrusted(X509Certificate[]chain,StringauthType)throwsCertificateException{}publicvoidcheckServerTrusted(X509Certificate[]chain,StringauthType)throwsCertificateException{}publicX509Certificate[]getAcceptedIssuers(){returnnewX509Certificate[]{};}}}
解决方案
解决方案二:
没做过这方面的,帮顶啦。祝楼主好运...
解决方案三:
百度上查查吧,贴上错误搜索