热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HttpClient使用java--POST方式带参提交数据

本文章是用106试用通道用java发送短信的例子,复制代码就可使用,但前提有106试用账号和密码----------------------------------


本文章是用106试用通道用java发送短信  的例子,就可使用,但前提有106试用账号和密码

----------------------------------

package util;

 
 
 import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;


 public class Fixin {
  
  public static void sendMsg(String _phone,String _pwd,String _to,String _msg) throws HttpException, IOException{
   HttpClient client = new HttpClient();
   client.getHostConfiguration().setHost("106.ihuyi.cn",80,"http");


//post 带参提交数据
   PostMethod post = new PostMethod("/webservice/sms.php?method=Submit");
   post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//在头文件中设置转码
   NameValuePair[] op ={
     new NameValuePair("account", _phone),
     new NameValuePair("password", _pwd),
     new NameValuePair("mobile",_to),
     new NameValuePair("content",_msg),
     };
   post.setRequestBody(op);
   
   
   client.executeMethod(post);
   System.out.println("---------");
   // 打印服务器返回的状态
   System.out.println(post.getStatusLine());
   // 打印返回的信息
   System.out.println(post.getResponseBodyAsString());
   System.out.println(post.getURI());
   System.out.println("---------");
   
   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("utf-8"));
   //System.out.println(result);
   System.out.println("ok!");
   post.releaseConnection();
   
  }
 }

-------------------------------------------

package test;


import java.io.IOException;

import org.apache.commons.httpclient.HttpException;

import util.Fixin;

public class Test {
 
 private static String PHOnE= "cf_a6233976**";   // 106账号   http://sms.webchinese.cn/
 private static String PWD = "jie52019**";
 private static String TO = "183110093**";
 private static String MSG = "您的验证码是:888。请不要把验证码泄露给其他人。";
 
 public static void main(String[] args) throws HttpException, IOException {
  Fixin.sendMsg(PHONE, PWD, TO, MSG);
 }

}



推荐阅读
author-avatar
501917112_0de975_837
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有