HTTP interface call (Unicom SMS)

Interface documentationInterface documentationInterface documentation

Here Insert Picture Description

Code section


        String SpCode = "spCode";// 企业编号
        String LoginName = "LoginName"; // 用户名称
        String Password = "Password";// 用户密码
        String SerialNumber = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());// 流水号

        HttpClient client = new HttpClient();
        //请求地址
        PostMethod post = new PostMethod("http://***:**/sms/Api/Send.do");
        //设置请求编码
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=GBK");
        NameValuePair[] data = { new NameValuePair("SpCode", SpCode), new NameValuePair("LoginName", LoginName),
                        new NameValuePair("Password", Password), new NameValuePair("MessageContent", MessageContent),
                        new NameValuePair("UserNumber", UserNumber), new NameValuePair("SerialNumber", SerialNumber),
                        new NameValuePair("ScheduleTime", ""), new NameValuePair("ExtendAccessNum", ""),
                        new NameValuePair("f", "1") };
        //添加参数                
        post.setRequestBody(data);
        try {
        	//执行
            client.executeMethod(post);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //日志信息
        Header[] headers = post.getResponseHeaders();
        for (Header h : headers) {
            System.out.println(h.toString());
        }
        String result = "";
        JSONObject jsonObj = new JSONObject();
        try {
        	//接口返回信息
            result = new String(post.getResponseBody(), Charset.forName("GBK"));
            String[] results = result.split("&");
            //处理接口返回信息
            for (int i = 0; i < results.length; i++) {
                String[] result_mes = results[i].split("=");
                if (result_mes.length == 2) {
                    jsonObj.put(result_mes[0], result_mes[1]);
                }

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
		//释放底层HTTP连接以允许它被重用。
        post.releaseConnection();
        //返回到前台界面
        return "0".equals(jsonObj.get("result"));

    
Published 26 original articles · won praise 6 · views 2961

Guess you like

Origin blog.csdn.net/weixin_45676630/article/details/101206221