• 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();
            }
    

      

  • 相关阅读:
    Python基础05
    Python基础04
    Python基础03
    Python基础02
    Python基础01
    软件测试
    深入了解软件测试基础04
    深入了解软件测试基础03
    深入了解软件测试基础02
    深入了解软件测试基础01
  • 原文地址:https://www.cnblogs.com/Andrew520/p/10829843.html
Copyright © 2020-2023  润新知