Search in sources :

Example 1 with JHttp

use of j.http.JHttp in project JFramework by gugumall.

the class JUtilTimestamp method run.

/*
	 *  (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    JHttp jhttp = JHttp.getInstance();
    int loop = -1;
    boolean done = false;
    while (true) {
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
        }
        if (!done && (loop == -1 || loop == 30)) {
            try {
                JHttpContext context = new JHttpContext();
                context.addRequestHeader("Referer", "http://bjtime.cn/");
                context = jhttp.get(context, null, "http://bjtime.cn/header6.asp");
                String response = context != null && context.getStatus() == 200 ? context.getResponseText() : null;
                if (response != null) {
                    String t = "";
                    int start = response.indexOf("nyear=");
                    int end = -1;
                    if (start > 0) {
                        end = response.indexOf(";", start);
                        t += response.substring(start + 6, end) + "-";
                        start = response.indexOf("nmonth=");
                        end = response.indexOf(";", start);
                        String s = response.substring(start + 7, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s + "-";
                        start = response.indexOf("nday=");
                        end = response.indexOf(";", start);
                        s = response.substring(start + 5, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s + " ";
                        start = response.indexOf("nhrs=");
                        end = response.indexOf(";", start);
                        s = response.substring(start + 5, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s + ":";
                        start = response.indexOf("nmin=");
                        end = response.indexOf(";", start);
                        s = response.substring(start + 5, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s + ":";
                        start = response.indexOf("nsec=");
                        end = response.indexOf(";", start);
                        s = response.substring(start + 5, end);
                        if (s.length() == 1)
                            s = "0" + s;
                        t += s;
                        bjTime = Timestamp.valueOf(t).getTime() - System.currentTimeMillis();
                        done = true;
                        System.out.println("get bj time from bjtime.cn: " + bjTime + ", local time: " + System.currentTimeMillis());
                    }
                }
            } catch (Exception e) {
            // e.printStackTrace();
            }
            loop = 0;
        }
        loop++;
    }
}
Also used : JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp)

Example 2 with JHttp

use of j.http.JHttp in project JFramework by gugumall.

the class RegionGetter method fromBaidu.

/**
 * @throws Exception
 */
private static void fromBaidu() throws Exception {
    JHttp http = JHttp.getInstance();
    HttpClient client = http.createClient();
    String continentId = "0";
    String countryId = "1";
    String provinceId = null;
    String cityId = null;
    String countyId = null;
    JUtilTextWriter log = new JUtilTextWriter(new File("F:\\work\\JFramework_v2.0\\doc\\regions.sql"), "UTF-8");
    log.addLine("use jframework;");
    String s = JDFSFile.read(new File("F:\\work\\JFramework_v2.0\\doc\\淘宝地域\\tdist_py.js"), "GBK");
    String[] ls = JUtilString.getTokens(s, "\n");
    for (int i = 0; i < ls.length; i++) {
        if (ls[i].indexOf(":[") < 0)
            continue;
        String id = ls[i].substring(0, ls[i].indexOf(":["));
        id = JUtilString.replaceAll(id, "'", "");
        String info = ls[i].substring(ls[i].indexOf(":[") + 2, ls[i].indexOf("]"));
        info = JUtilString.replaceAll(info, "'", "");
        String[] cells = info.split(",");
        String name = cells[0];
        String pid = cells[1];
        String nameEn = cells[2];
        int idi = Integer.parseInt(id);
        // 外国城市不要
        if (idi >= 1000 && idi < 110000)
            continue;
        // 国家
        if (idi < 1000) {
            log.addLine("insert into j_country values ('" + id + "','" + pid + "','','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','',0,'T');");
            System.out.println("insert into j_country values ('" + id + "','" + pid + "','','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','',0,'T');");
        } else if (id.endsWith("0000")) {
            // 省
            if (provinceId == null || !provinceId.equals(id)) {
                provinceId = id;
            }
            String nameShort = name;
            nameShort = nameShort.replaceAll("市", "");
            nameShort = nameShort.replaceAll("维吾尔自治区", "");
            nameShort = nameShort.replaceAll("壮族自治区", "");
            nameShort = nameShort.replaceAll("回族自治区", "");
            nameShort = nameShort.replaceAll("自治区", "");
            log.addLine(" insert into j_province values ('" + id + "','" + pid + "','" + continentId + "','" + name + "','" + nameShort + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','',0,'','T');");
            System.out.println(" insert into j_province values ('" + id + "','" + pid + "','" + continentId + "','" + name + "','" + nameShort + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','',0,'','T');");
        } else if (pid.equals(provinceId)) {
            if (cityId == null || !cityId.equals(id)) {
                cityId = id;
            }
            String areaCode = "";
            String postCode = "";
            String[] data = (String[]) codes.get(id);
            if (data != null) {
                areaCode = data[0];
                postCode = data[1];
            }
            log.addLine("  insert into j_city values ('" + id + "','" + pid + "','" + countryId + "','" + continentId + "','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','" + areaCode + "',0,'" + postCode + "','T');");
            System.out.println("  insert into j_city values ('" + id + "','" + pid + "','" + countryId + "','" + continentId + "','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','" + areaCode + "',0,'" + postCode + "','T');");
        } else {
            if (countyId == null || !countyId.equals(id)) {
                countyId = id;
            }
            String areaCode = "";
            String postCode = "";
            String[] data = (String[]) codes.get(id);
            if (data != null) {
                areaCode = data[0];
                postCode = data[1];
            }
            log.addLine("   insert into j_county values ('" + id + "','" + pid + "','" + provinceId + "','" + countryId + "','" + continentId + "','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','" + areaCode + "',0,'" + postCode + "','T');");
            System.out.println("   insert into j_county values ('" + id + "','" + pid + "','" + provinceId + "','" + countryId + "','" + continentId + "','" + name + "','" + JUtilString.toZhTw(name) + "','" + nameEn + "','" + areaCode + "',0,'" + postCode + "','T');");
            JHttpContext context = new JHttpContext();
            context.addRequestHeader("Referer", "http://buy.taobao.com/auction/buy_now.jhtml");
            String zones = http.postResponse(context, client, "http://lsp.wuliu.taobao.com/locationservice/addr/output_address_town.do?l1=" + provinceId + "&l2=" + cityId + "&l3=" + countyId + "&_ksTS=1396014988093_459&callback=jsonp460", null, "UTF-8");
            zones = zones.substring(zones.indexOf("result:{") + "result:{".length());
            zones = JUtilString.replaceAll(zones, "],", "],\r\n");
            String[] zs = JUtilString.getTokens(zones, "\r\n");
            for (int j = 0; j < zs.length; j++) {
                if (zs[j].indexOf(":[") < 0)
                    continue;
                String id2 = zs[j].substring(0, zs[j].indexOf(":["));
                id2 = JUtilString.replaceAll(id2, "'", "");
                String info2 = zs[j].substring(zs[j].indexOf(":[") + 2, zs[j].indexOf("]"));
                info2 = JUtilString.replaceAll(info2, "'", "");
                String[] cells2 = info2.split(",");
                String name2 = cells2[0];
                String nameEn2 = cells2[2];
                log.addLine("    insert into j_zone values ('" + id2 + "','" + countyId + "','" + cityId + "','" + provinceId + "','" + countryId + "','" + continentId + "','" + name2 + "','" + JUtilString.toZhTw(name2) + "','" + nameEn2 + "','',0,'','T');");
                System.out.println("    insert into j_zone values ('" + id2 + "','" + countyId + "','" + cityId + "','" + provinceId + "','" + countryId + "','" + continentId + "','" + name2 + "','" + JUtilString.toZhTw(name2) + "','" + nameEn2 + "','',0,'','T');");
            }
        }
    }
    System.exit(0);
}
Also used : JUtilTextWriter(j.util.JUtilTextWriter) JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp) HttpClient(org.apache.http.client.HttpClient) JUtilString(j.util.JUtilString) JDFSFile(j.fs.JDFSFile) File(java.io.File)

Example 3 with JHttp

use of j.http.JHttp in project JFramework by gugumall.

the class Temp method main.

/**
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    // String s="{\"method\":\"add\", \"params\": [1,2], \"id\": 1}";
    String s = "{\"method\":\"getdifficulty\", \"id\": 1}";
    JHttpContext context = new JHttpContext();
    JHttp http = JHttp.getInstance();
    HttpClient client = http.createClient("112.74.65.173", 8332, "http", "gugucoinuser", "gugucoinpass");
    // context.setRequestBody(s);
    s = http.postResponse(context, null, "http://112.74.65.173:8332/", null);
    // URL: http://user:pass@localhost:8332
    System.out.println(s);
}
Also used : JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp) HttpClient(org.apache.http.client.HttpClient)

Example 4 with JHttp

use of j.http.JHttp in project JFramework by gugumall.

the class LoginAgent method login.

/**
 * 向SSO Server 为以fromClientId为ID的SSO Client提供用户验证
 * @param fromClientId
 * @param request
 * @return
 */
LoginResult login(String fromClientId, HttpServletRequest request) {
    // 关联的SSO Client信息
    Client parent = parent();
    String _Interface = this.Interface;
    // 如使用相对地址,自动生成绝对地址
    if (!_Interface.startsWith("http"))
        _Interface = parent.getUrlDefault() + _Interface;
    // 登录请求参数及相关详情
    String[] paraNamesAndQueryString = getParameters(request);
    String paraNames = paraNamesAndQueryString[0];
    String paraValues = paraNamesAndQueryString[1];
    String queryString = paraNamesAndQueryString[2];
    // 把所有登录请求参数全部原样传送过去
    if (!paraNames.equals("")) {
        if (_Interface.indexOf("&") > 0 || _Interface.indexOf("?") > 0)
            _Interface += "&" + queryString.substring(1);
        else
            _Interface += queryString;
    }
    // md5拼串 - 登录请求参数名(多个逗号分隔)+按参数名顺序累加的各参数值+提供验证的SSO Client与SSO Server交互的passport(密钥)
    // 收到请求时,应该先获取_parameters参数,并按照其指明的参数顺序拼接各参数值,然后按照上述一样的方式得出md5值,与_verifier参数值相同才能通过验证
    // 详见j.app.sso.SSOClient.login()
    // System.out.println("1 - "+paraNames+paraValues+parent.getPassport());
    String md5 = JUtilMD5.MD5EncodeToHex(paraNames + paraValues + parent.getPassport());
    if (_Interface.indexOf("&") > 0 || _Interface.indexOf("?") > 0)
        _Interface += "&_parameters=" + paraNames + "&_verifier=" + md5;
    else
        _Interface += "?_parameters=" + paraNames + "&_verifier=" + md5;
    _Interface += "&" + Constants.SSO_USER_IP + "=" + JHttp.getRemoteIp(request);
    String userDomain = JUtilString.getProtocal(request.getRequestURL().toString()) + "://" + JUtilString.getHost(request.getRequestURL().toString());
    _Interface += "&" + Constants.SSO_USER_DOMAIN + "=" + JUtilString.encodeURI(userDomain, SysConfig.sysEncoding);
    // log.log("login agent interface:"+_Interface,-1);
    // 发起验证请求并获得结果
    String result = null;
    try {
        JHttp http = JHttp.getInstance();
        JHttpContext context = http.get(null, null, _Interface, SysConfig.sysEncoding);
        // log.log("context.getStatus():"+context.getStatus(), -1);
        result = context.getStatus() == 200 ? context.getResponseText() : null;
        context.finalize();
        context = null;
    } catch (Exception e) {
        log.log("agent login error,login agent interface - " + _Interface, -1);
        e.printStackTrace();
        result = null;
        log.log(e, Logger.LEVEL_FATAL);
    }
    if (result == null) {
        LoginResult loginResult = new LoginResult();
        loginResult.setResult(LoginResult.RESULT_ERROR);
        return loginResult;
    }
    if (result.indexOf("<" + Constants.SSO_USER_ID + ">") > -1 && result.indexOf("<" + Constants.SSO_LOGIN_RESULT_CODE + ">") > -1 && result.indexOf("<" + Constants.SSO_LOGIN_RESULT_MSG + ">") > -1) {
        Document doc = null;
        try {
            doc = JUtilDom4j.parseString(result, "UTF-8");
        } catch (Exception e) {
            log.log(e, Logger.LEVEL_ERROR);
            LoginResult loginResult = new LoginResult();
            loginResult.setResult(LoginResult.RESULT_ERROR);
            return loginResult;
        }
        Element root = doc.getRootElement();
        String sysId = root.elementText(Constants.SSO_SYS_ID);
        String machineId = root.elementText(Constants.SSO_MACHINE_ID);
        String userId = root.elementText(Constants.SSO_USER_ID);
        String code = root.elementText(Constants.SSO_LOGIN_RESULT_CODE);
        String msg = root.elementText(Constants.SSO_LOGIN_RESULT_MSG);
        LoginResult loginResult = new LoginResult();
        loginResult.setSysId(sysId);
        loginResult.setMachineId(machineId);
        loginResult.setUserId(userId);
        loginResult.setResult(Integer.parseInt(code));
        loginResult.setResultMsg(msg);
        List ms = root.element("messages").elements();
        for (int i = 0; i < ms.size(); i++) {
            Element m = (Element) ms.get(i);
            loginResult.setMessage(m.getName(), JObject.intSequence2String(m.getText()));
        }
        root = null;
        doc = null;
        return loginResult;
    } else {
        LoginResult loginResult = new LoginResult();
        loginResult.setResult(LoginResult.RESULT_ERROR);
        return loginResult;
    }
}
Also used : JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp) Element(org.dom4j.Element) List(java.util.List) JUtilString(j.util.JUtilString) Document(org.dom4j.Document)

Example 5 with JHttp

use of j.http.JHttp in project JFramework by gugumall.

the class TestUpload method pipe.

/**
 * @param jsession
 * @param session
 * @param request
 * @param response
 * @throws Exception
 */
public void pipe(JSession jsession, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
    JHttp jhttp = JHttp.getInstance();
    jsession.resultString = jhttp.pipe(request, "POST", "www.jstudio.me", 80, "/jstudio/upload.handler?request=upload&from=" + request.getParameter("from"));
}
Also used : JHttp(j.http.JHttp)

Aggregations

JHttp (j.http.JHttp)7 JHttpContext (j.http.JHttpContext)6 JUtilString (j.util.JUtilString)3 HttpClient (org.apache.http.client.HttpClient)3 JDFSFile (j.fs.JDFSFile)1 JUtilTextWriter (j.util.JUtilTextWriter)1 File (java.io.File)1 RemoteException (java.rmi.RemoteException)1 List (java.util.List)1 InitialContext (javax.naming.InitialContext)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1