info.smartkit.hairy_batman.demo.MoniterWechatBrowser.java Source code

Java tutorial

Introduction

Here is the source code for info.smartkit.hairy_batman.demo.MoniterWechatBrowser.java

Source

/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * All rights reserved.
 */
package info.smartkit.hairy_batman.demo;

/**
 * TODO: DOCUMENT ME!
 * 
 * @author yangboz
 */

import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * ?(?)
 */
public class MoniterWechatBrowser {
    public static void main(String[] args) {
        String url = "http://weixin.sogou.com/gzh?openid=oIWsFt_Ri_gqjARIY_shVuqjc3Zo";
        String userAgent = "Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; GT-I9300 Build/JZO54K) "
                + "AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.2.380";
        String html = getHttpClientHtml(url, "UTF-8", userAgent);
        System.out.println(html);
    }

    /**
     * ?URLhtml?
     */
    @SuppressWarnings("deprecation")
    public static String getHttpClientHtml(String url, String code, String userAgent) {
        String html = null;
        @SuppressWarnings("deprecation")
        DefaultHttpClient httpClient = new DefaultHttpClient();// httpClient
        HttpGet httpget = new HttpGet(url);// get?URL
        // Pause for 4 seconds
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
            System.out.println(e1.toString());
        }
        //
        httpget.setHeader("User-Agent", userAgent);
        try {
            // responce
            HttpResponse responce = httpClient.execute(httpget);
            // ?
            int returnCode = responce.getStatusLine().getStatusCode();
            // 200? ?
            if (returnCode == HttpStatus.SC_OK) {
                // 
                HttpEntity entity = responce.getEntity();
                if (entity != null) {
                    html = new String(EntityUtils.toString(entity));// html??
                }
            }
        } catch (Exception e) {
            System.out.println("");
            e.printStackTrace();
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return html;
    }
}