zoukankan      html  css  js  c++  java
  • java发送短信--httpclient方式

    post方式(这种方式比较普遍)
    
        实现代码如下:
    
        //发送短信post方式
        public void sendMSGByPost(String phone) throws HttpException, IOException{
            HttpClient client = new HttpClient();
            PostMethod post = new PostMethod(URLSTR);
            post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
            NameValuePair[] data ={ //配置参数
                    new NameValuePair("username", USR),
                    new NameValuePair("password", PSD),
                    new NameValuePair("phone",phone),
                    new NameValuePair("message",MSG),
                    new NameValuePair("epid", EPID),
                    new NameValuePair("linkid",LINKID),
                    new NameValuePair("subcode",SUBCODE)
            };
            post.setRequestBody(data);
            client.executeMethod(post);//发送请求
            //以下为返回信息
            Header[] headers = post.getResponseHeaders();
            int statusCode = post.getStatusCode();//状态码
            System.out.println("statusCode:"+statusCode);
            for(Header h : headers)
            {
            System.out.println(h.toString());
            }
            String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
            System.out.println(result);
            post.releaseConnection();
        }
    
       以上方式比较固定,其实很多都是用这种方式,有兴趣的可以试试中国网建的:http://www.smschinese.cn/api.shtml(各种代码示例)
    

      另外一种写法

    HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));
            GetMethod method = new GetMethod();
            try {
                URI base = new URI(url, false);
                method.setURI(new URI(base, "HttpBatchSendSM", false));
                method.setQueryString(new NameValuePair[] {
                        new NameValuePair("account", account),
                        new NameValuePair("pswd", pswd),
                        new NameValuePair("mobile", mobile),
                        new NameValuePair("needstatus", String.valueOf(needstatus)),
                        new NameValuePair("msg", msg),
                        new NameValuePair("extno", extno),
                });
                int result = client.executeMethod(method);
                if (result == HttpStatus.SC_OK) {
                    InputStream in = method.getResponseBodyAsStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = in.read(buffer)) != -1) {
                        baos.write(buffer, 0, len);
                    }
                    return URLDecoder.decode(baos.toString(), "UTF-8");
                } else {
                    throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
                }
            } finally {
                method.releaseConnection();
            }
    

      

  • 相关阅读:
    [题解] LuoguP1587 [NOI2016]循环之美
    [题解] LuoguP3705 [SDOI2017]新生舞会
    [题解] LuoguP3702 [SDOI2017]序列计数
    [题解] LuoguP6476 [NOI Online 2 提高组]涂色游戏
    [题解] LuoguP4240 毒瘤之神的考验
    [题解] LuoguP6156简单题
    [题解] LuoguP6055 [RC-02] GCD
    [题解] LuoguP5050 【模板】多项式多点求值
    AtCoder Grand Contest 028题解
    Codeforces Round #421 (Div. 1) 题解
  • 原文地址:https://www.cnblogs.com/Andrew520/p/10829843.html
Copyright © 2011-2022 走看看