如何登录这样的网站? [英] how to login to a site like this?

查看:126
本文介绍了如何登录这样的网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Android应用程序登录到我的互联网服务提供商的网站,然后看我的3G数据消费特征。

(这是一个HTTPS的网站: HTTPS://www.simyo。 ES / simyo /门户网站/客户/动作/我的面板

我能够读取与code,我把在这篇文章的结尾的网站。

不过......我不知道该怎么介绍我的用户名/密码,然后就提交。

你知道吗?

(对不起,我的英文不好)

感谢您

EasySSLSocketFactory.java

 包com.javimoya.test;

进口java.io.IOException异常;
进口的java.net.InetAddress;
进口java.net.InetSocketAddress;
进口的java.net.Socket;
进口的java.net.UnknownHostException;

进口javax.net.ssl​​.SSLContext;
进口javax.net.ssl​​.SSLSocket中;
进口javax.net.ssl​​.TrustManager;

进口org.apache.http.conn.ConnectTimeoutException;
进口org.apache.http.conn.scheme.LayeredSocketFactory;
进口org.apache.http.conn.scheme.SocketFactory;
进口org.apache.http.params.HttpConnectionParams;
进口org.apache.http.params.HttpParams;

公共类EasySSLSocketFactory实现的SocketFactory,LayeredSocketFactory
{
    私人的SSL连接的SSL连接= NULL;

    私有静态的SSLContext createEasySSLContext()抛出IOException异常
    {
      尝试
      {
        的SSL连接上下文= SSLContext.getInstance(TLS);
        context.init(空,新的TrustManager [] {新EasyX509TrustManager(空)},NULL);
        返回范围内;
      }
      赶上(例外五)
      {
        抛出新IOException异常(e.getMessage());
      }
    }

    专用的SSL连接getSSLContext()抛出IOException异常
    {
      如果(this.sslcontext == NULL)
      {
        this.sslcontext = createEasySSLContext();
      }
      返回this.sslcontext;
    }

    / **
     * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket中,java.lang.String中,INT,
     * java.net.InetAddress中,INT,org.apache.http.params.HttpParams)
     * /
    公共插座connectSocket(插座袜子,
                                    字符串主机,
                                    INT端口,
                                    InetAddress类将localAddress,
                                    INT将localPort
                                    的HttpParams PARAMS)

                抛出IOException异常,UnknownHostException异常,ConnectTimeoutException
    {
      INT connTimeout = HttpConnectionParams.getConnectionTimeout(PARAMS);
      INT soTimeout = HttpConnectionParams.getSoTimeout(PARAMS);
      InetSocketAddress remoteAddress =新的InetSocketAddress(主机,端口);
      SSLSocket sslsock =(SSLSocket)(!?(袜子= NULL)袜子:中的createSocket());

      如果((将localAddress = NULL)||(将localPort>!0))
      {
        //我们需要明确地绑定
        如果(将localPort℃,)
        {
          将localPort = 0; //表示任何
        }
        InetSocketAddress ISA =新的InetSocketAddress(将localAddress,将localPort);
        sslsock.bind(ISA);
      }

      sslsock.connect(remoteAddress,connTimeout);
      sslsock.setSoTimeout(soTimeout);
      返回sslsock;
    }

    / **
     * @see org.apache.http.conn.scheme.SocketFactory#中的createSocket()
     * /
    公共插座中的createSocket()抛出IOException异常{
        返回getSSLContext()在getSocketFactory()中的createSocket()。
    }

    / **
     * @see org.apache.http.conn.scheme.SocketFactory#的isSecure(java.net.Socket中)
     * /
    公共布尔的isSecure(Socket套接字)抛出:IllegalArgumentException  -  {
        返回true;
    }

    / **
     * @see org.apache.http.conn.scheme.LayeredSocketFactory#中的createSocket(java.net.Socket中,java.lang.String中,INT,
     *布尔)
     * /
    公共插座中的createSocket(Socket套接字,
                                   字符串主机,
                                   INT端口,
                                   布尔自动关闭)抛出IOException异常,
                                                             UnknownHostException异常
    {
      返回getSSLContext()在getSocketFactory()中的createSocket(插座,主机,端口自动关闭)。;
    }

    // ------------------------------------------------ -------------------
    //的javadoc在org.apache.http.conn.scheme.SocketFactory说:
    //两者的Object.Equals()和Object.hash code()必须重写
    //对于一些连接管理器的正确操作
    // ------------------------------------------------ -------------------

    公共布尔等于(obj对象){
        返程((OBJ = NULL)及!&安培; obj.getClass()等于(EasySSLSocketFactory.class));
    }

    公众诠释哈希code(){
        返回EasySSLSocketFactory.class.hash code();
    }
}
 

EasyX509TrustManager.java

 包com.javimoya.test;

进口java.security.KeyStore中;
进口java.security.KeyStoreException;
进口java.security.NoSuchAlgorithmException;
进口java.security.cert.CertificateException;
进口java.security.cert.X509Certificate;
进口java.util.Date;
进口javax.net.ssl​​.TrustManager;
进口javax.net.ssl​​.TrustManagerFactory;
进口javax.net.ssl​​.X509TrustManager;

公共类EasyX509TrustManager实现X509TrustManager
{
    私人X509TrustManager standardTrustManager = NULL;

    / **
     *构造EasyX509TrustManager。
     * /
    公共EasyX509TrustManager(密钥库密钥库)抛出抛出:NoSuchAlgorithmException,KeyStoreException
    {
      超();
      的TrustManagerFactory工厂= TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
      factory.init(密钥库);
      的TrustManager [] trustmanagers = factory.getTrustManagers();
      如果(trustmanagers.length == 0)
      {
        抛出新的抛出:NoSuchAlgorithmException(不信任管理器找到了);
      }
      this.standardTrustManager =(X509TrustManager)trustmanagers [0];
    }

    / **
     * @see javax.net.ssl​​.X509TrustManager#checkClientTrusted(X509证书[],字符串的authType)
     * /
    公共无效checkClientTrusted(x509证书[]证书,字符串的authType)抛出CertificateException
    {
      standardTrustManager.checkClientTrusted(证书的authType);
    }

    / **
     * @see javax.net.ssl​​.X509TrustManager#checkServerTrusted(X509证书[],字符串的authType)
     * /
    公共无效checkServerTrusted(x509证书[]证书,字符串的authType)抛出CertificateException
    {
    //清理证书链,并建立一个新的。
        //从理论上说,我们不应该这样做,但各种web服务器
        //实际上是错误配置有-乱序证书或
        //过期自行颁发的根证书。
        INT链长= certificates.length;
        如果(certificates.length→1)
        {
          // 1.清洁获颁证书链。
          //我们从终端实体证书开始,跟踪下来匹配
          //发行人栏和主题字段,直到我们无法继续。
          //这有助于当证书失灵或
          //某些证书是不相关的网站。
          INT currIndex;
          对于(currIndex = 0; currIndex< certificates.length ++ currIndex)
          {
            布尔foundNext = FALSE;
            对于(INT nextIndex = currIndex + 1;
                           nextIndex< certificates.length;
                           ++ nextIndex)
            {
              如果(证书[currIndex] .getIssuerDN()。等于(
                            证书[nextIndex] .getSubjectDN()))
              {
                foundNext = TRUE;
                //交换证书,使0〜currIndex + 1是正确的顺序
                如果(nextIndex!= currIndex + 1)的
                {
                  x509证书tempCertificate =证书[nextIndex]
                  证书[nextIndex] =证书[currIndex + 1];
                  证书[currIndex + 1] = tempCertificate;
                }
                打破;
            }
            }
            如果(foundNext!)破;
      }

          // 2.我们考试如果最后跟踪证书是自颁发并已过期。
          //如果是这样,我们把它剩下的传递给checkServerTrusted(),希望我们可以
          //有一个类似的,但过期的受信任的根。
          链长= currIndex + 1;
          x509证书lastCertificate =证书[链长 -  1];
          日期现在=新的日期();
          如果(lastCertificate.getSubjectDN()。等于(lastCertificate.getIssuerDN())
                  &功放;&安培; now.after(lastCertificate.getNotAfter()))
          {
            --chainLength;
          }
      }

    standardTrustManager.checkServerTrusted(证书的authType);
    }

    / **
     * @see javax.net.ssl​​.X509TrustManager#getAcceptedIssuers()
     * /
    公共x509证书[] getAcceptedIssuers()
    {
      返回this.standardTrustManager.getAcceptedIssuers();
    }
}
 

MyHttpClient.java

 包com.javimoya.test;

进口org.apache.http.conn.ClientConnectionManager;
进口org.apache.http.conn.scheme.PlainSocketFactory;
进口org.apache.http.conn.scheme.Scheme;
进口org.apache.http.conn.scheme.SchemeRegistry;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.impl.conn.SingleClientConnManager;
进口org.apache.http.params.HttpParams;

进口android.content.Context;

公共类MyHttpClient扩展DefaultHttpClient
{
  最后上下文的背景下;

  公共MyHttpClient(的HttpParams hparms,上下文语境)
  {
    超(hparms);
    this.context =背景;
  }

  @覆盖
  保护ClientConnectionManager createClientConnectionManager(){
    SchemeRegistry注册表=新SchemeRegistry();
    registry.register(新计划(HTTP,PlainSocketFactory.getSocketFactory(),80));

    //注册端口443我们的SSLSocketFactory与我们的密钥库
    //到的ConnectionManager
    registry.register(新计划(https开头,新EasySSLSocketFactory(),443));

    //http://blog.synyx.de/2010/06/android-and-self-signed-ssl-certificates/
    返回新SingleClientConnManager(getParams()方法,登记);
  }
}
 

MySSLSocketFactory.java

 包com.javimoya.test;

进口java.io.IOException异常;
进口的java.net.Socket;
进口的java.net.UnknownHostException;
进口java.security.KeyManagementException;
进口java.security.KeyStore中;
进口java.security.KeyStoreException;
进口java.security.NoSuchAlgorithmException;
进口java.security.UnrecoverableKeyException;
进口java.security.cert.CertificateException;
进口java.security.cert.X509Certificate;

进口javax.net.ssl​​.SSLContext;
进口javax.net.ssl​​.TrustManager;
进口javax.net.ssl​​.X509TrustManager;

进口org.apache.http.conn.ssl.SSLSocketFactory;

公共类MySSLSocketFactory扩展的SSLSocketFactory {
    的SSL连接的SSL连接= SSLContext.getInstance(TLS);

    公共MySSLSocketFactory(密钥库信任库)抛出抛出:NoSuchAlgorithmException,KeyManagementException,KeyStoreException,UnrecoverableKeyException {
        超(信任库);

        的TrustManager TM =新X509TrustManager(){
            公共无效checkClientTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
            }

            公共无效checkServerTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
            }

            公共x509证书[] getAcceptedIssuers(){
                返回null;
            }
        };

        sslContext.init(空,新的TrustManager [] {} TM,NULL);
    }

    @覆盖
    公共插座中的createSocket(Socket套接字,字符串主机,INT端口,布尔自动关闭)抛出IOException异常,UnknownHostException异常{
        返回sslContext.getSocketFactory()中的createSocket(插座,主机,端口自动关闭)。
    }

    @覆盖
    公共插座中的createSocket()抛出IOException异常{
        返回sslContext.getSocketFactory()中的createSocket()。
    }
}
 

TestActivity.java

 包com.javimoya.test;

进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;

进口org.apache.http.Htt presponse;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.params.BasicHttpParams;
进口org.apache.http.params.HttpConnectionParams;
进口org.apache.http.params.HttpParams;

进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.TextView;

公共类TestActivity延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        TextView的电视=(TextView中)findViewById(R.id.tv1);


        的HttpParams httpParameters =新BasicHttpParams();
        //毫秒设置超时,直到建立了连接。
        INT timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
        //设置默认套接字超时(SO_TIMEOUT)
        //毫秒这是超时等待数据。
        INT timeoutSocket = 10000;
        HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);

        //实例化自定义的HttpClient
        HttpClient的客户端=新MyHttpClient(httpParameters,
                                             getApplicationContext());
      // HTTPGET请求=新HTTPGET(https://eu.battle.net/login/en/login.xml);
      HTTPGET请求=新HTTPGET(https://www.simyo.es/simyo/portal/customer/action/my-panel);

        BufferedReader中的= NULL;
        尝试
        {
        HTT presponse响应= client.execute(要求);
        在=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));

        StringBuffer的SB =新的StringBuffer();
        串线=;
        串NL = System.getProperty(line.separator);
        而((行= in.readLine())!= NULL)
        {
          sb.append(行+ NL);
        }
        附寄();
        字符串页= sb.toString();
        //System.out.println(page);

        tv.setText(页);
            }
        赶上(ClientProtocolException E)
            {
                e.printStackTrace();
            }
        赶上(IOException异常E)
        {
                e.printStackTrace();
            }
        最后
        {
        如果(在!= NULL)
        {
          尝试
          {
            附寄();
          }
          赶上(IOException异常E)
          {
            e.printStackTrace();
          }
        }
        }
    }
}
 

解决方案

  HTTPGET请求=新HTTPGET(https://www.simyo.es/simyo/portal/customer/action/my-panel);
 

做一个

  HttpPost请求=新HttpPost(https://www.simyo.es/simyo/portal/j_security_check);
名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(2);
nameValuePairs.add(新BasicNameValuePair(为j_username,PUT_USERNAME_HERE));
nameValuePairs.add(新BasicNameValuePair(为j_password,PUT_PASSWORD_HERE));
request.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
client.execute(要求);
 

I want to create an android app to login to my internet provider website and then read my 3g data comsumption.

(It's a https website: https://www.simyo.es/simyo/portal/customer/action/my-panel)

I'm able to read the website with the code I put at the end of this post.

But... I don't know how to introduce my username/password and then do submit.

Any idea?

(Sorry for my poor english)

Thank you

EasySSLSocketFactory.java

package com.javimoya.test;

import java.io.IOException;  
import java.net.InetAddress;  
import java.net.InetSocketAddress;  
import java.net.Socket;  
import java.net.UnknownHostException;  

import javax.net.ssl.SSLContext;  
import javax.net.ssl.SSLSocket;  
import javax.net.ssl.TrustManager;  

import org.apache.http.conn.ConnectTimeoutException;  
import org.apache.http.conn.scheme.LayeredSocketFactory;  
import org.apache.http.conn.scheme.SocketFactory;  
import org.apache.http.params.HttpConnectionParams;  
import org.apache.http.params.HttpParams;  

public class EasySSLSocketFactory implements SocketFactory, LayeredSocketFactory 
{  
    private SSLContext sslcontext = null;  

    private static SSLContext createEasySSLContext() throws IOException 
    {  
      try
      {  
        SSLContext context = SSLContext.getInstance("TLS");  
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);  
        return context;  
      }
      catch (Exception e) 
      {  
        throw new IOException(e.getMessage());  
      }  
    }  

    private SSLContext getSSLContext() throws IOException 
    {  
      if (this.sslcontext == null) 
      {  
        this.sslcontext = createEasySSLContext();  
      }  
      return this.sslcontext;  
    }  

    /** 
     * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, java.lang.String, int, 
     *      java.net.InetAddress, int, org.apache.http.params.HttpParams) 
     */  
    public Socket connectSocket(Socket sock,
                                    String host,
                                    int port, 
                                    InetAddress localAddress,
                                    int localPort,
                                    HttpParams params) 

                throws IOException, UnknownHostException, ConnectTimeoutException 
    {  
      int connTimeout = HttpConnectionParams.getConnectionTimeout(params);  
      int soTimeout = HttpConnectionParams.getSoTimeout(params);  
      InetSocketAddress remoteAddress = new InetSocketAddress(host, port);  
      SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());  

      if ((localAddress != null) || (localPort > 0)) 
      {  
        // we need to bind explicitly  
        if (localPort < 0) 
        {  
          localPort = 0; // indicates "any"  
        }  
        InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);  
        sslsock.bind(isa);  
      }  

      sslsock.connect(remoteAddress, connTimeout);  
      sslsock.setSoTimeout(soTimeout);  
      return sslsock;    
    }  

    /** 
     * @see org.apache.http.conn.scheme.SocketFactory#createSocket() 
     */  
    public Socket createSocket() throws IOException {  
        return getSSLContext().getSocketFactory().createSocket();  
    }  

    /** 
     * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket) 
     */  
    public boolean isSecure(Socket socket) throws IllegalArgumentException {  
        return true;  
    }  

    /** 
     * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket, java.lang.String, int, 
     *      boolean) 
     */  
    public Socket createSocket(Socket socket,
                                   String host, 
                                   int port,
                                   boolean autoClose) throws IOException,  
                                                             UnknownHostException 
    {  
      return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);  
    }  

    // -------------------------------------------------------------------  
    // javadoc in org.apache.http.conn.scheme.SocketFactory says :  
    // Both Object.equals() and Object.hashCode() must be overridden  
    // for the correct operation of some connection managers  
    // -------------------------------------------------------------------  

    public boolean equals(Object obj) {  
        return ((obj != null) && obj.getClass().equals(EasySSLSocketFactory.class));  
    }  

    public int hashCode() {  
        return EasySSLSocketFactory.class.hashCode();  
    }  
}

EasyX509TrustManager.java

package com.javimoya.test;

import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Date;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class EasyX509TrustManager implements X509TrustManager 
{  
    private X509TrustManager standardTrustManager = null;  

    /** 
     * Constructor for EasyX509TrustManager. 
     */  
    public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException 
    {  
      super();  
      TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());  
      factory.init(keystore);  
      TrustManager[] trustmanagers = factory.getTrustManagers();  
      if (trustmanagers.length == 0) 
      {  
        throw new NoSuchAlgorithmException("no trust manager found");  
      }  
      this.standardTrustManager = (X509TrustManager) trustmanagers[0];  
    }  

    /** 
     * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType) 
     */  
    public void checkClientTrusted(X509Certificate[] certificates, String authType) throws CertificateException 
    {  
      standardTrustManager.checkClientTrusted(certificates, authType);  
    }  

    /** 
     * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType) 
     */  
    public void checkServerTrusted(X509Certificate[] certificates, String authType) throws CertificateException 
    {  
    // Clean up the certificates chain and build a new one.
        // Theoretically, we shouldn't have to do this, but various web servers
        // in practice are mis-configured to have out-of-order certificates or
        // expired self-issued root certificate.
        int chainLength = certificates.length;
        if (certificates.length > 1) 
        {
          // 1. we clean the received certificates chain.
          // We start from the end-entity certificate, tracing down by matching
          // the "issuer" field and "subject" field until we can't continue.
          // This helps when the certificates are out of order or
          // some certificates are not related to the site.
          int currIndex;
          for (currIndex = 0; currIndex < certificates.length; ++currIndex) 
          {
            boolean foundNext = false;
            for (int nextIndex = currIndex + 1;
                           nextIndex < certificates.length;
                           ++nextIndex) 
            {
              if (certificates[currIndex].getIssuerDN().equals(
                            certificates[nextIndex].getSubjectDN())) 
              {
                foundNext = true;
                // Exchange certificates so that 0 through currIndex + 1 are in proper order
                if (nextIndex != currIndex + 1) 
                {
                  X509Certificate tempCertificate = certificates[nextIndex];
                  certificates[nextIndex] = certificates[currIndex + 1];
                  certificates[currIndex + 1] = tempCertificate;
                }
                break;
            }
            }
            if (!foundNext) break;
      }

          // 2. we exam if the last traced certificate is self issued and it is expired.
          // If so, we drop it and pass the rest to checkServerTrusted(), hoping we might
          // have a similar but unexpired trusted root.
          chainLength = currIndex + 1;
          X509Certificate lastCertificate = certificates[chainLength - 1];
          Date now = new Date();
          if (lastCertificate.getSubjectDN().equals(lastCertificate.getIssuerDN())
                  && now.after(lastCertificate.getNotAfter())) 
          {
            --chainLength;
          }
      } 

    standardTrustManager.checkServerTrusted(certificates, authType);    
    }  

    /** 
     * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() 
     */  
    public X509Certificate[] getAcceptedIssuers() 
    {  
      return this.standardTrustManager.getAcceptedIssuers();  
    }    
}  

MyHttpClient.java

package com.javimoya.test;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.params.HttpParams;

import android.content.Context;

public class MyHttpClient extends DefaultHttpClient 
{    
  final Context context;

  public MyHttpClient(HttpParams hparms, Context context)
  {
    super(hparms);
    this.context = context;     
  }

  @Override
  protected ClientConnectionManager createClientConnectionManager() {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    // Register for port 443 our SSLSocketFactory with our keystore
    // to the ConnectionManager
    registry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

    //http://blog.synyx.de/2010/06/android-and-self-signed-ssl-certificates/
    return new SingleClientConnManager(getParams(), registry);
  }
}

MySSLSocketFactory.java

package com.javimoya.test;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ssl.SSLSocketFactory;

public class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("TLS");

    public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
        super(truststore);

        TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sslContext.init(null, new TrustManager[] { tm }, null);
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
        return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
    }

    @Override
    public Socket createSocket() throws IOException {
        return sslContext.getSocketFactory().createSocket();
    }
}

TestActivity.java

package com.javimoya.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView)findViewById(R.id.tv1);


        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        int timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 10000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        // Instantiate the custom HttpClient
        HttpClient client = new MyHttpClient(httpParameters,
                                             getApplicationContext());
      //HttpGet request = new HttpGet("https://eu.battle.net/login/en/login.xml");
      HttpGet request = new HttpGet("https://www.simyo.es/simyo/portal/customer/action/my-panel");

        BufferedReader in = null;
        try 
        {
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null)
        {
          sb.append(line + NL);
        }
        in.close();
        String page = sb.toString();
        //System.out.println(page);

        tv.setText(page);
            }
        catch (ClientProtocolException e) 
            {
                e.printStackTrace();
            }
        catch (IOException e) 
        {
                e.printStackTrace();
            }
        finally
        {
        if (in != null) 
        {
          try
          {
            in.close();
          }
          catch (IOException e) 
          {
            e.printStackTrace();
          }
        }                       
        }
    }      
}

解决方案

Before

HttpGet request = new HttpGet("https://www.simyo.es/simyo/portal/customer/action/my-panel");

Do a

HttpPost request = new HttpPost("https://www.simyo.es/simyo/portal/j_security_check");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("j_username", "PUT_USERNAME_HERE"));
nameValuePairs.add(new BasicNameValuePair("j_password", "PUT_PASSWORD_HERE"));
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
client.execute(request);

这篇关于如何登录这样的网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆