您的位置:首页 > 编程语言 > Java开发

java导入163联系人

2013-11-14 11:58 267 查看
现在在许多SNS中都有导入邮箱联系人的功能,以下的代码功能就是如何取得163邮箱账号的联系人

更多的导入联系人代码,可在此处下载:http://code.google.com/p/jcontactfetcher/

首先是工具类

1.CommonUtil类,现在只有初始化log4j方法

[java] view
plaincopy

package org.gc.contact.util;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;

public final class CommonUtil {

static {

initLog4J();

}

private CommonUtil() {}

private static void initLog4J() {

InputStream in = CommonUtil.class.getResourceAsStream("/log4j.properties");

Properties props = new Properties();

try {

props.load(in);

PropertyConfigurator.configure(props);

} catch(IOException ex) {

System.out.println("Log4JLogger Create Exception:/n" + ex.getMessage());

}

}

}

2.EncodingUtil类,编码相关

[java] view
plaincopy

package org.gc.contact.util;

public class EncodingUtil {

public static final String DEFAULT_ENCODING = "UTF-8";

private EncodingUtil() {}

}

3.RegexpUtil类,正则表达式工具

[java] view
plaincopy

package org.gc.contact.util;

import java.util.regex.Pattern;

public final class RegexpUtil {

private static final String REGEXP_FORM_INPUT_VALUE = "<//s*input(?://s+.*)*//s+value=/"?([^//s/"]*)/"?";

private static final String REGEXP_HOST_FROM_URI = "http://([^/]+)/.*";

private static final String REGEXP_JS_REDIRECTION = "location//.replace//(/"(.+)/"//)";

private static final Pattern PATTERN_FORM_INPUT_VALUE = Pattern.compile(REGEXP_FORM_INPUT_VALUE);

private static final Pattern PATTERN_HOST_FROM_URI = Pattern.compile(REGEXP_HOST_FROM_URI);

private static final Pattern PATTERN_JS_REDIRECTION = Pattern.compile(REGEXP_JS_REDIRECTION);

private RegexpUtil() {}

public static Pattern getFormInputValueRegexp() {

return PATTERN_FORM_INPUT_VALUE;

}

public static Pattern getHostFromURIRegexp() {

return PATTERN_HOST_FROM_URI;

}

public static Pattern getJSRedirectionRegexp() {

return PATTERN_JS_REDIRECTION;

}

}

接着是实体类

4.联系人类

[java] view
plaincopy

package org.gc.contact.model;

import java.io.Serializable;

public class ContactInfo implements Serializable {

private static final long serialVersionUID = 5530203198421462152L;

private final String username;

private final String address; //email & qq etc.

public ContactInfo(String username, String address) {

this.username = username;

this.address = address;

}

public String getUsername() {

return username;

}

public String getAddress() {

return address;

}

public String toString() {

return "[" + username + "," + address + "]";

}

}

异常类

5.错误消息

[java] view
plaincopy

package org.gc.contact.exception;

public final class ExceptionMessage {

public static final String PROTOCOL_MSG = "Don't find a match; may be the regular expression error, may be the protocol changed.";

private ExceptionMessage() {}

}

6.HttpBrowserException,浏览器异常

[java] view
plaincopy

package org.gc.contact.exception;

@SuppressWarnings("serial")

public class HttpBrowserException extends Exception {

public HttpBrowserException() {

super();

}

public HttpBrowserException(String error) {

super(error);

}

public HttpBrowserException(Throwable t) {

super(t);

}

public HttpBrowserException(String error, Throwable t) {

super(error, t);

}

}

7.HttpServerException,服务器异常

[java] view
plaincopy

package org.gc.contact.exception;

@SuppressWarnings("serial")

public class HttpServerException extends Exception {

public HttpServerException() {

super();

}

public HttpServerException(String error) {

super(error);

}

public HttpServerException(Throwable t) {

super(t);

}

public HttpServerException(String error, Throwable t) {

super(error, t);

}

}

8.ProtocolException,协议异常

[java] view
plaincopy

package org.gc.contact.exception;

@SuppressWarnings("serial")

public class ProtocolException extends Exception {

public ProtocolException() {

super();

}

public ProtocolException(Throwable t) {

super(t);

}

public ProtocolException(String error) {

super(error);

}

public ProtocolException(String error, Throwable t) {

super(error, t);

}

}

核心类

9.HttpBrowser类,模拟浏览器的类,有get,post行为

[java] view
plaincopy

package org.gc.contact.http;

import java.io.IOException;

import java.util.Date;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import org.apache.commons.httpclient.Cookie;

import org.apache.commons.httpclient.Header;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.HttpMethod;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.httpclient.NameValuePair;

import org.apache.commons.httpclient.URIException;

import org.apache.commons.httpclient.cookie.CookiePolicy;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.httpclient.params.HttpMethodParams;

import org.apache.commons.httpclient.protocol.Protocol;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.gc.contact.exception.ExceptionMessage;

import org.gc.contact.exception.HttpBrowserException;

import org.gc.contact.exception.HttpServerException;

import org.gc.contact.util.EncodingUtil;

import org.gc.contact.util.RegexpUtil;

@SuppressWarnings("deprecation")

public class HttpBrowser {

static {

SSLUtilities.trustAllHttpsCertificates();

SSLUtilities.trustAllHostnames();

Protocol myhttps = new Protocol("https", new SSLFactory (), 443);

Protocol.registerProtocol("https", myhttps);

}

private static final Log logger = LogFactory.getLog(HttpBrowser.class);

private HttpClient httpClient;

protected String encoding;

protected int bufferLength = 4096;

protected String lastVisitUrl;

public HttpBrowser() {

this(EncodingUtil.DEFAULT_ENCODING);

}

public HttpBrowser(String encoding) {

this.encoding = encoding;

httpClient = new HttpClient();

httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);

httpClient.getParams().setParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);

}

//----------- callable methods -------------//

public HttpResponse doGet(String url) throws HttpBrowserException, HttpServerException {

return doGet(url, "");

}

public HttpResponse doGet(String url, String referer) throws HttpBrowserException, HttpServerException {

try {

GetMethod getMethod = new GetMethod(url);

setHttpRequestHeader(getMethod);

if (referer != null && referer.trim().length() != 0) {

getMethod.setRequestHeader("Referer", referer);

}

logHttpGetRequest(getMethod);

int status =httpClient.executeMethod(getMethod);

String strResp = getMethod.getResponseBodyAsString();

byte[] byteResp = getMethod.getResponseBody();

String respEnc = getResponseEncoding(getMethod);

logHttpResponse(getMethod, strResp);

getMethod.releaseConnection();

//http:301,302,303,307

if (status == HttpStatus.SC_MOVED_PERMANENTLY ||

status == HttpStatus.SC_MOVED_TEMPORARILY ||

status == HttpStatus.SC_SEE_OTHER ||

status == HttpStatus.SC_TEMPORARY_REDIRECT) {

Header locationHeader = getMethod.getResponseHeader("Location");

String location = locationHeader.getValue();

if (logger.isDebugEnabled()) {

logger.debug("Redirect To Location = " + location);

}

if (location.startsWith("http")) {

return doGet(location);

} else {

return doGet("http://" + getResponseHost(getMethod) + location);

}

} else if (status == HttpStatus.SC_OK) { //http:200

return new HttpResponse(getMethod.getURI().toString(), byteResp, respEnc);

} else {

throw new HttpServerException("Server Exception[code=" + status + "]");

}

} catch (HttpException e) {

throw new HttpBrowserException(e);

} catch (IOException e) {

throw new HttpBrowserException(e);

}

}

public HttpResponse doPost(String url, NameValuePair[] params) throws HttpBrowserException, HttpServerException {

return doPost(url, params, "");

}

public HttpResponse doPost(String url, NameValuePair[] params, String referer) throws HttpBrowserException, HttpServerException {

try {

PostMethod postMethod = new PostMethod(url);

setHttpRequestHeader(postMethod);

if (referer != null && referer.trim().length() != 0) {

postMethod.setRequestHeader("Referer", referer);

}

postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

postMethod.setRequestBody(params);

logHttpPostRequest(postMethod);

int status = httpClient.executeMethod(postMethod);

String strResp = postMethod.getResponseBodyAsString();

byte[] byteResp = postMethod.getResponseBody();

String respEnc = getResponseEncoding(postMethod);

logHttpResponse(postMethod, strResp);

postMethod.releaseConnection();

//http:301,302,303,307

if (status == HttpStatus.SC_MOVED_PERMANENTLY ||

status == HttpStatus.SC_MOVED_TEMPORARILY ||

status == HttpStatus.SC_SEE_OTHER ||

status == HttpStatus.SC_TEMPORARY_REDIRECT) {

Header locationHeader = postMethod.getResponseHeader("Location");

String location = locationHeader.getValue();

if (logger.isDebugEnabled()) {

logger.debug("Redirect To Location = " + location);

}

if (location.startsWith("http")) {

return doGet(location);

} else {

return doGet("http://" + getResponseHost(postMethod) + location);

}

} else if (status == HttpStatus.SC_OK) { //http:200

return new HttpResponse(postMethod.getURI().toString(), byteResp, respEnc);

} else {

throw new HttpServerException("Server Exception[code=" + status + "]");

}

} catch (HttpException e) {

throw new HttpBrowserException(e);

} catch (IOException e) {

throw new HttpBrowserException(e);

}

}

public Cookie[] getCurrentCookies() {

Cookie[] cookies = httpClient.getState().getCookies();

return cookies;

}

protected void setHttpRequestHeader(HttpMethod method) {

method.setRequestHeader("Accept",

"text/html,application/xhtml+xml,application/xml,application/json,image/jpeg,image/gif,*/*");

method.setRequestHeader("Accept-Language", "zh-cn");

method.setRequestHeader(

"User-Agent",

"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");

method.setRequestHeader("Accept-Charset", encoding);

method.setRequestHeader("Keep-Alive", "300");

method.setRequestHeader("Connection", "Keep-Alive");

method.setRequestHeader("Cache-Control", "no-cache");

}

//------------- log methods ----------------//

private void logHttpGetRequest(HttpMethod method) {

try {

if (logger.isDebugEnabled()) {

logger.debug("/n/n============= HTTP Request Start =============");

logger.debug("HTTP Get Request URL ==>/n" + method.getURI().toString());

logger.debug("HTTP Get Request Headers ==>/n" + getHttpRequestHeader(method));

logger.debug("HTTP Get Request Cookies ==>/n" + getHttpCookie());

logger.debug("HTTP Get Request QueryString ==>/n" + method.getQueryString());

logger.debug("============= HTTP Request End =============/n/n");

}

} catch (URIException e) {

logger.error(e);

}

}

private void logHttpPostRequest(PostMethod method) {

try {

if (logger.isDebugEnabled()) {

logger.debug("/n/n============= HTTP Request Start =============");

logger.debug("HTTP Post Request URL ==>/n" + method.getURI().toString());

logger.debug("HTTP Post Request Headers ==>/n" + getHttpRequestHeader(method));

logger.debug("HTTP Post Request Cookies ==>/n" + getHttpCookie());

logger.debug("HTTP Post Request QueryString ==>/n" + method.getQueryString());

logger.debug("HTTP Post Request Body ==>/n" + getHttpRequestBody(method));

logger.debug("============= HTTP Request End =============/n/n");

}

} catch (URIException e) {

logger.error("URIException", e);

}

}

private void logHttpResponse(HttpMethod method, String strResp) {

try {

if (logger.isDebugEnabled()) {

logger.debug("/n/n============= HTTP Response Start =============");

logger.debug("HTTP Response URL ==>/n" + method.getURI().toString());

logger.debug("HTTP Response Headers ==>/n" + getHttpResponseHeader(method));

logger.debug("HTTP Response Cookies ==>/n" + getHttpCookie());

logger.debug("HTTP Response Body ==>/n" + strResp);

logger.debug("============= HTTP Response End =============/n/n");

}

} catch (URIException e) {

logger.error("URIException", e);

}

}

//---------- util methods -------------//

private String getResponseEncoding(HttpMethod method) {

Header header = method.getResponseHeader("Content-Type");

String encoding = EncodingUtil.DEFAULT_ENCODING;

if (header != null) {

if (logger.isDebugEnabled()) {

logger.debug("Content-Type=" + header.getValue());

}

if (header != null) {

String value = header.getValue();

int idx1 = value.indexOf("charset=");

if (idx1 > -1) {

encoding = value.substring(idx1 + 8);

if (logger.isDebugEnabled()) {

logger.debug("Response Encoding=" + encoding);

}

}

}

}

return encoding;

}

private String getResponseHost(HttpMethod method) {

try {

Pattern p = RegexpUtil.getHostFromURIRegexp();

String url = method.getURI().toString();

Matcher matcher = p.matcher(url);

if (!matcher.find()) {

String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=ResponseHost,Regexp=" + p.pattern() + "]";

logger.error(msg);

}

if (logger.isDebugEnabled()) {

logger.debug("Host=" + matcher.group(1));

}

return matcher.group(1);

} catch (Exception e) {

logger.error(e);

}

return "";

}

private String getHttpRequestBody(PostMethod method) {

StringBuilder strBody = new StringBuilder();

NameValuePair[] pairs = method.getParameters();

for (NameValuePair pair : pairs) {

String name = pair.getName();

String value = pair.getValue();

strBody.append(name + "=" + value + ";");

}

return strBody.toString();

}

private String getHttpCookie() {

StringBuilder strHeader = new StringBuilder();

Cookie[] cookies = httpClient.getState().getCookies();

for (Cookie cookie : cookies) {

String domain = cookie.getDomain();

String path = cookie.getPath();

String name = cookie.getName();

String value = cookie.getValue();

Date expired = cookie.getExpiryDate();

boolean isSecure = cookie.getSecure();

strHeader.append("domain=" + domain + ";");

strHeader.append("path=" + path + ";");

strHeader.append(name + "=" + value + ";");

if (expired != null) {

strHeader.append("expired=" + expired.toGMTString() + ";");

}

strHeader.append("isSecure=" + isSecure+ "/n");

}

return strHeader.toString();

}

private String getHttpRequestHeader(HttpMethod method) {

StringBuilder strHeader = new StringBuilder();

Header[] headers = method.getRequestHeaders();

for (Header header : headers) {

String name = header.getName();

String value = header.getValue();

strHeader.append(name + "=" + value + ";");

}

return strHeader.toString();

}

private String getHttpResponseHeader(HttpMethod method) {

StringBuilder strHeader = new StringBuilder();

Header[] headers = method.getResponseHeaders();

for (Header header : headers) {

String name = header.getName();

String value = header.getValue();

strHeader.append(name + "=" + value + ";");

}

return strHeader.toString();

}

}

10.HttpResponse类,http的回复结果类

[java] view
plaincopy

package org.gc.contact.http;

import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.gc.contact.util.EncodingUtil;

public class HttpResponse {

private static final Log logger = LogFactory.getLog(HttpResponse.class);

private final String responseUrl;

private final byte[] bodyAsBytes;

private final String encoding;

public HttpResponse(String responseUrl, byte[] bodyAsBytes) {

this(responseUrl, bodyAsBytes, EncodingUtil.DEFAULT_ENCODING);

}

public HttpResponse(String responseUrl, byte[] bodyAsBytes, String encoding) {

this.responseUrl = responseUrl;

this.bodyAsBytes = bodyAsBytes;

this.encoding = encoding;

}

public String getResponseUrl() {

return responseUrl;

}

public String getBodyAsString() {

try {

if (logger.isDebugEnabled()) {

logger.debug("Convert Encoding=" + encoding);

}

return new String(bodyAsBytes, encoding);

} catch (UnsupportedEncodingException e) {

logger.error("Encoding Error[encoding=" + encoding + "]", e);

try {

return new String(bodyAsBytes, EncodingUtil.DEFAULT_ENCODING);

} catch (UnsupportedEncodingException e1) {

logger.error("Encoding Error[encoding=" + EncodingUtil.DEFAULT_ENCODING + "]", e);

return new String(bodyAsBytes);

}

}

}

public byte[] getBodyAsBytes() {

return bodyAsBytes;

}

public InputStream getBodyAsStream() {

return new ByteArrayInputStream(bodyAsBytes);

}

}

11.SSLFactory类,用于https

[java] view
plaincopy

package org.gc.contact.http;

import java.io.IOException;

import java.net.InetAddress;

import java.net.InetSocketAddress;

import java.net.Socket;

import java.net.SocketAddress;

import java.net.UnknownHostException;

import java.security.KeyManagementException;

import java.security.NoSuchAlgorithmException;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import javax.net.SocketFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

import org.apache.commons.httpclient.ConnectTimeoutException;

import org.apache.commons.httpclient.params.HttpConnectionParams;

import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

public class SSLFactory implements SecureProtocolSocketFactory {

static {

System.out.println(">>>> SSLFactory Init <<<<");

}

private SSLContext sslcontext = null;

private SSLContext createSSLContext() {

SSLContext sslcontext=null;

try {

sslcontext = SSLContext.getInstance("SSL");

sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (KeyManagementException e) {

e.printStackTrace();

}

return sslcontext;

}

private SSLContext getSSLContext() {

if (this.sslcontext == null) {

this.sslcontext = createSSLContext();

}

return this.sslcontext;

}

public Socket createSocket(Socket socket, String host, int port, boolean autoClose)

throws IOException, UnknownHostException {

return getSSLContext().getSocketFactory().createSocket(

socket,

host,

port,

autoClose

);

}

public Socket createSocket(String host, int port) throws IOException,

UnknownHostException {

return getSSLContext().getSocketFactory().createSocket(

host,

port

);

}

public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)

throws IOException, UnknownHostException {

return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);

}

public Socket createSocket(String host, int port, InetAddress localAddress,

int localPort, HttpConnectionParams params) throws IOException,

UnknownHostException, ConnectTimeoutException {

if (params == null) {

throw new IllegalArgumentException("Parameters may not be null");

}

int timeout = params.getConnectionTimeout();

SocketFactory socketfactory = getSSLContext().getSocketFactory();

if (timeout == 0) {

return socketfactory.createSocket(host, port, localAddress, localPort);

} else {

Socket socket = socketfactory.createSocket();

SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);

SocketAddress remoteaddr = new InetSocketAddress(host, port);

socket.bind(localaddr);

socket.connect(remoteaddr, timeout);

return socket;

}

}

private static class TrustAnyTrustManager implements X509TrustManager {

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

}

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

}

public X509Certificate[] getAcceptedIssuers() {

return new X509Certificate[]{};

}

}

}

12.SSLUtilities类,用于https

[java] view
plaincopy

package org.gc.contact.http;

import java.security.GeneralSecurityException;

import java.security.SecureRandom;

import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.HttpsURLConnection;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

/**

* This class provide various static methods that relax X509 certificate and

* hostname verification while using the SSL over the HTTP protocol.

*

* @author Francis Labrie

*/

public final class SSLUtilities {

/**

* Hostname verifier for the Sun's deprecated API.

*

* @deprecated see {@link #_hostnameVerifier}.

*/

private static com.sun.net.ssl.HostnameVerifier __hostnameVerifier;

/**

* Thrust managers for the Sun's deprecated API.

*

* @deprecated see {@link #_trustManagers}.

*/

private static com.sun.net.ssl.TrustManager[] __trustManagers;

/**

* Hostname verifier.

*/

private static HostnameVerifier _hostnameVerifier;

/**

* Thrust managers.

*/

private static TrustManager[] _trustManagers;

/**

* Set the default Hostname Verifier to an instance of a fake class that

* trust all hostnames. This method uses the old deprecated API from the

* com.sun.ssl package.

*

* @deprecated see {@link #_trustAllHostnames()}.

*/

private static void __trustAllHostnames() {

// Create a trust manager that does not validate certificate chains

if (__hostnameVerifier == null) {

__hostnameVerifier = new _FakeHostnameVerifier();

} // if

// Install the all-trusting host name verifier

com.sun.net.ssl.HttpsURLConnection

.setDefaultHostnameVerifier(__hostnameVerifier);

} // __trustAllHttpsCertificates

/**

* Set the default X509 Trust Manager to an instance of a fake class that

* trust all certificates, even the self-signed ones. This method uses the

* old deprecated API from the com.sun.ssl package.

*

* @deprecated see {@link #_trustAllHttpsCertificates()}.

*/

private static void __trustAllHttpsCertificates() {

com.sun.net.ssl.SSLContext context;

// Create a trust manager that does not validate certificate chains

if (__trustManagers == null) {

__trustManagers = new com.sun.net.ssl.TrustManager[] { new _FakeX509TrustManager() };

} // if

// Install the all-trusting trust manager

try {

context = com.sun.net.ssl.SSLContext.getInstance("SSL");

context.init(null, __trustManagers, new SecureRandom());

} catch (GeneralSecurityException gse) {

throw new IllegalStateException(gse.getMessage());

} // catch

com.sun.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(context

.getSocketFactory());

} // __trustAllHttpsCertificates

/**

* Return true if the protocol handler property java. protocol.handler.pkgs

* is set to the Sun's com.sun.net.ssl. internal.www.protocol deprecated

* one, false otherwise.

*

* @return true if the protocol handler property is set to the Sun's

* deprecated one, false otherwise.

*/

private static boolean isDeprecatedSSLProtocol() {

return ("com.sun.net.ssl.internal.www.protocol".equals(System

.getProperty("java.protocol.handler.pkgs")));

} // isDeprecatedSSLProtocol

/**

* Set the default Hostname Verifier to an instance of a fake class that

* trust all hostnames.

*/

private static void _trustAllHostnames() {

// Create a trust manager that does not validate certificate chains

if (_hostnameVerifier == null) {

_hostnameVerifier = new FakeHostnameVerifier();

} // if

// Install the all-trusting host name verifier:

HttpsURLConnection.setDefaultHostnameVerifier(_hostnameVerifier);

} // _trustAllHttpsCertificates

/**

* Set the default X509 Trust Manager to an instance of a fake class that

* trust all certificates, even the self-signed ones.

*/

private static void _trustAllHttpsCertificates() {

SSLContext context;

// Create a trust manager that does not validate certificate chains

if (_trustManagers == null) {

_trustManagers = new TrustManager[] { new FakeX509TrustManager() };

} // if

// Install the all-trusting trust manager:

try {

context = SSLContext.getInstance("SSL");

context.init(null, _trustManagers, new SecureRandom());

} catch (GeneralSecurityException gse) {

throw new IllegalStateException(gse.getMessage());

} // catch

HttpsURLConnection.setDefaultSSLSocketFactory(context

.getSocketFactory());

} // _trustAllHttpsCertificates

/**

* Set the default Hostname Verifier to an instance of a fake class that

* trust all hostnames.

*/

public static void trustAllHostnames() {

// Is the deprecated protocol setted?

if (isDeprecatedSSLProtocol()) {

__trustAllHostnames();

} else {

_trustAllHostnames();

} // else

} // trustAllHostnames

/**

* Set the default X509 Trust Manager to an instance of a fake class that

* trust all certificates, even the self-signed ones.

*/

public static void trustAllHttpsCertificates() {

// Is the deprecated protocol setted?

if (isDeprecatedSSLProtocol()) {

__trustAllHttpsCertificates();

} else {

_trustAllHttpsCertificates();

} // else

} // trustAllHttpsCertificates

/**

* This class implements a fake hostname verificator, trusting any host

* name. This class uses the old deprecated API from the com.sun. ssl

* package.

*

* @author Francis Labrie

*

* @deprecated see {@link SSLUtilities.FakeHostnameVerifier}.

*/

public static class _FakeHostnameVerifier implements

com.sun.net.ssl.HostnameVerifier {

/**

* Always return true, indicating that the host name is an acceptable

* match with the server's authentication scheme.

*

* @param hostname

* the host name.

* @param session

* the SSL session used on the connection to host.

* @return the true boolean value indicating the host name is trusted.

*/

public boolean verify(String hostname, String session) {

return (true);

} // verify

} // _FakeHostnameVerifier

/**

* This class allow any X509 certificates to be used to authenticate the

* remote side of a secure socket, including self-signed certificates. This

* class uses the old deprecated API from the com.sun.ssl package.

*

* @author Francis Labrie

*

* @deprecated see {@link SSLUtilities.FakeX509TrustManager}.

*/

public static class _FakeX509TrustManager implements

com.sun.net.ssl.X509TrustManager {

/**

* Empty array of certificate authority certificates.

*/

private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};

/**

* Always return true, trusting for client SSL chain peer certificate

* chain.

*

* @param chain

* the peer certificate chain.

* @return the true boolean value indicating the chain is trusted.

*/

public boolean isClientTrusted(X509Certificate[] chain) {

return (true);

} // checkClientTrusted

/**

* Always return true, trusting for server SSL chain peer certificate

* chain.

*

* @param chain

* the peer certificate chain.

* @return the true boolean value indicating the chain is trusted.

*/

public boolean isServerTrusted(X509Certificate[] chain) {

return (true);

} // checkServerTrusted

/**

* Return an empty array of certificate authority certificates which are

* trusted for authenticating peers.

*

* @return a empty array of issuer certificates.

*/

public X509Certificate[] getAcceptedIssuers() {

return (_AcceptedIssuers);

} // getAcceptedIssuers

} // _FakeX509TrustManager

/**

* This class implements a fake hostname verificator, trusting any host

* name.

*

* @author Francis Labrie

*/

public static class FakeHostnameVerifier implements HostnameVerifier {

/**

* Always return true, indicating that the host name is an acceptable

* match with the server's authentication scheme.

*

* @param hostname

* the host name.

* @param session

* the SSL session used on the connection to host.

* @return the true boolean value indicating the host name is trusted.

*/

public boolean verify(String hostname, javax.net.ssl.SSLSession session) {

return (true);

} // verify

} // FakeHostnameVerifier

/**

* This class allow any X509 certificates to be used to authenticate the

* remote side of a secure socket, including self-signed certificates.

*

* @author Francis Labrie

*/

public static class FakeX509TrustManager implements X509TrustManager {

/**

* Empty array of certificate authority certificates.

*/

private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};

/**

* Always trust for client SSL chain peer certificate chain with any

* authType authentication types.

*

* @param chain

* the peer certificate chain.

* @param authType

* the authentication type based on the client certificate.

*/

public void checkClientTrusted(X509Certificate[] chain, String authType) {

} // checkClientTrusted

/**

* Always trust for server SSL chain peer certificate chain with any

* authType exchange algorithm types.

*

* @param chain

* the peer certificate chain.

* @param authType

* the key exchange algorithm used.

*/

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

} // checkServerTrusted

/**

* Return an empty array of certificate authority certificates which are

* trusted for authenticating peers.

*

* @return a empty array of issuer certificates.

*/

public X509Certificate[] getAcceptedIssuers() {

return (_AcceptedIssuers);

} // getAcceptedIssuers

} // FakeX509TrustManager

} // SSLUtilities

下面就是取得联系人的过程了

13.IContactFetcher接口

[java] view
plaincopy

package org.gc.contact.fetcher;

import java.util.List;

import org.gc.contact.exception.HttpBrowserException;

import org.gc.contact.exception.HttpServerException;

import org.gc.contact.exception.ProtocolException;

import org.gc.contact.model.ContactInfo;

public interface IContactFetcher {

void doLogin() throws HttpBrowserException, HttpServerException, ProtocolException;

List<ContactInfo> fetchContactInfo() throws HttpBrowserException, HttpServerException, ProtocolException;

}

14.AbstractContactFetcher类

[java] view
plaincopy

package org.gc.contact.fetcher;

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.gc.contact.exception.ExceptionMessage;

import org.gc.contact.exception.ProtocolException;

import org.gc.contact.http.HttpBrowser;

import org.gc.contact.util.RegexpUtil;

public abstract class AbstractContactFetcher implements IContactFetcher {

protected final Log logger = LogFactory.getLog(getClass());

protected final HttpBrowser browser;

protected String username;

protected String password;

public AbstractContactFetcher(String username, String password) {

this.username = username;

this.password = password;

browser = new HttpBrowser();

}

protected String getJSRedirection(String htmlContent) throws ProtocolException {

Pattern p = RegexpUtil.getJSRedirectionRegexp();

Matcher matcher = p.matcher(htmlContent);

if (!matcher.find()) {

String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=JSRedirection,Regexp=" + p.pattern() + "]";

logger.error(msg);

throw new ProtocolException(msg);

}

if (logger.isDebugEnabled()) {

logger.debug("Redirection URL = " + matcher.group(1));

}

return matcher.group(1);

}

protected String getFormInputValue(String name, String htmlContent) throws ProtocolException {

Pattern p = RegexpUtil.getFormInputValueRegexp();

int index = htmlContent.indexOf(name);

int prePos = index - 400 > 0 ? index - 400 : 0;

int postPos = index + 400 < htmlContent.length() ? index + 400 : htmlContent.length();

int start = htmlContent.substring(prePos, index).lastIndexOf("<input") + prePos;

int end = htmlContent.substring(index, postPos).indexOf(">") + index;

String htmlInput = htmlContent.substring(start, end + 1);

if (logger.isDebugEnabled()) {

logger.debug("HtmlInput=" + htmlInput);

}

Matcher matcher = p.matcher(htmlInput);

if (!matcher.find()) {

String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=FormInputValue,Regexp=" + p.pattern() + "]";

logger.error(msg);

throw new ProtocolException(msg);

}

if (logger.isDebugEnabled()) {

logger.debug("Name = " + name + ";value = " + matcher.group(1));

}

return matcher.group(1);

}

protected String getHost(String url) throws ProtocolException {

Pattern p = RegexpUtil.getHostFromURIRegexp();

Matcher matcher = p.matcher(url);

if (!matcher.find()) {

String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=Host,Regexp=" + p.pattern() + "]";

logger.error(msg);

throw new ProtocolException(msg);

}

if (logger.isDebugEnabled()) {

logger.debug("Host = " + matcher.group(1));

}

return matcher.group(1);

}

protected List<String> getValue(Pattern p, String htmlContent) throws ProtocolException {

List<String> data = new ArrayList<String>();

Matcher matcher = p.matcher(htmlContent);

while (matcher.find()) {

try {

data.add(matcher.group(1));

if (logger.isDebugEnabled()) {

logger.debug("Matched String=" + matcher.group(1));

}

} catch (Exception e) {

String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=getValue,Regexp=" + p.pattern() + "]";

logger.error(msg);

throw new ProtocolException(msg);

}

}

if (data.size() == 0) {

String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=getValue,Regexp=" + p.pattern() + "]";

logger.warn(msg);

}

return data;

}

}

15.取163的联系人

[java] view
plaincopy

package org.gc.contact.fetcher;

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Pattern;

import org.apache.commons.httpclient.NameValuePair;

import org.gc.contact.exception.HttpBrowserException;

import org.gc.contact.exception.HttpServerException;

import org.gc.contact.exception.ProtocolException;

import org.gc.contact.http.HttpResponse;

import org.gc.contact.model.ContactInfo;

/**

* @163.com邮箱取地址本过程

* 1. loginUrl

* 2. window.location.replace("url")

* 3. fetch address list

*

*/

public class Mail163ContactFetcher extends AbstractContactFetcher {

private final String loginUrl = "http://reg.163.com/login.jsp?type=1&product=mail163&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D35";

/**

* fetch url

* http://g1a61.mail.163.com/jy3/address/addrlist.jsp?sid=SBmtqsXXQmCXunRFLtXXXfowbRxMPUUX&gid=all
*/

private String fetchUrl;

private final String REGEXP_USER = "<td class=/"Ibx_Td_addrName/"><a href="/" mce_href="/""addrdetail.jsp//?sid=.+&gid=all/" title=/".+/">(.+)</a></td>";

private final String REGEXP_EMAIL = "<td class=/"Ibx_Td_addrEmail/"><a href="/" mce_href="/""addrdetail.jsp//?sid=.+&gid=all/" title=/".+/">(.+)</a></td>";

private final Pattern PATTERN_USER = Pattern.compile(REGEXP_USER);

private final Pattern PATTERN_EMAIL = Pattern.compile(REGEXP_EMAIL);

public Mail163ContactFetcher(String username, String password) {

super(username, password);

}

@Override

public void doLogin() throws HttpBrowserException, HttpServerException, ProtocolException {

NameValuePair[] loginParams = makeLoginParams();

HttpResponse redirectPage = browser.doPost(loginUrl, loginParams);

String redirectUrl = getJSRedirection(redirectPage.getBodyAsString());

HttpResponse mainPage = browser.doGet(redirectUrl);

fetchUrl = mainPage.getResponseUrl();

}

@Override

public List<ContactInfo> fetchContactInfo() throws HttpBrowserException, HttpServerException, ProtocolException {

List<ContactInfo> contacts = new ArrayList<ContactInfo>();

String contactUrl = fetchUrl.replace("main.jsp", "address/addrlist.jsp");

contactUrl += "&gid=all";

HttpResponse contactPage = browser.doGet(contactUrl);

String html = contactPage.getBodyAsString();

//为了匹配,每个td一行

html = html.replace("</td>", "</td>/n");

List<String> username = getValue(PATTERN_USER, html);

List<String> email = getValue(PATTERN_EMAIL, html);

for (int i = 0; i < username.size(); i++) {

contacts.add(new ContactInfo(username.get(i), email.get(i)));

}

return contacts;

}

private NameValuePair[] makeLoginParams() {

return new NameValuePair[] {

new NameValuePair("username", username),

new NameValuePair("password", password)

};

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: