com173

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cloudssaas.test.email;

import com.cloudssaas.beans.ResponseBean;
import com.cloudssaas.util.HttpUtil;
import com.cloudssaas.util.URLCoderUtil;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author csx
 */
public class Test3 {

    public static void main(String[] args) {
        //System.out.println(Integer.toString((int)(Math.random()*10)));
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String guest_sidUrl = "https://mail.173.com/site/guest_captcha";//获取guest_sid
        ResponseBean responseBean = HttpUtil.doGetBean(httpclient, guest_sidUrl, "https://mail.173.com/user/new");
        String guest_sid = "";
        String vcodetext = "";
        for (Cookie c : responseBean.getCookie()) {
            System.out.println(c.getName());
            if (c.getName().equals("guest_sid")) {
                guest_sid = c.getValue();
                break;
            }
        }
        System.out.println(guest_sid);

        String codeUrl = "https://mail.173.com/site/guest_captcha";//https://mail.173.com/site/guest_captcha
        //System.out.println(codeUrl);
        byte[] bt = HttpUtil.getImage(httpclient, codeUrl, "https://mail.173.com/user/new","guest_sid="+guest_sid);
        Object object = JOptionPane.showInputDialog(null, "", "输入验证码", JOptionPane.PLAIN_MESSAGE, new ImageIcon(bt), null, "");
        System.out.println(object.toString());

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("captcha", URLCoderUtil.encode(object.toString(), "UTF-8")));

        HttpPost httppost = new HttpPost("https://mail.173.com/user/ajax_new_check");
        //httppost.setHeader("Accept-Encoding", "gzip,deflate,sdch");
        httppost.setHeader("Host", "mail.173.com");
        httppost.setHeader("Content-Type","application/json");
        httppost.setHeader("Referer", "https://mail.173.com/user/new");
        httppost.setHeader("Origin", "https://mail.173.com");
        httppost.setHeader("Cookie", "guest_sid="+guest_sid);
        httppost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36");

        HttpEntity entity = null;
        String postbody = null;
        try {
            httppost.setEntity(new UrlEncodedFormEntity(params));
            CloseableHttpResponse getResponse = httpclient.execute(httppost);
            entity = getResponse.getEntity();
            postbody = EntityUtils.toString(entity, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Com173.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Com173.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(postbody);
    }
}


你可能感兴趣的:(httpclient,173.com)