zoukankan      html  css  js  c++  java
  • httpClient调用http接口

    package httpClient;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    import net.sf.json.JSONObject;
    
    public class TestSendSMS {
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            String uid = "12345678";
            String title = "test";
            String content = "test a content";
            String ret = sendSms(uid, title, content);
            System.out.println(ret);
    
            if (ret.indexOf("失败") < 0) {
                System.out.println("成功发送sms");
            } else {
                System.out.println("失败发送");
            }
    
        }
    
        public static String sendSms(String uid, String title, String content) {
            HttpClient httpclient = new DefaultHttpClient();
            String smsUrl = "http://localhost:9000/index";
            HttpPost httppost = new HttpPost(smsUrl);
            String strResult = "";
    
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                JSONObject jobj = new JSONObject();
                jobj.put("uid", uid);
                jobj.put("title", title);
                jobj.put("content", content);
                
                nameValuePairs.add(new BasicNameValuePair("msg", jobj.toString()));
                httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
    
                HttpResponse response = httpclient.execute(httppost);
                if (response.getStatusLine().getStatusCode() == 200) {
                    /* 读返回数据 */
                    String conResult = EntityUtils.toString(response.getEntity());
    
                    System.out.println(conResult);
                    // JSONObject sobj = new JSONObject();
                    // sobj = sobj.fromObject(conResult);
                    // String result = sobj.getString("result");
                    // String code = sobj.getString("code");
                    // if (result.equals("1")) {
                    // strResult += "发送成功";
                    // } else {
                    // strResult += "发送失败," + code;
                    // }
    
                } else {
                    String err = response.getStatusLine().getStatusCode() + "";
                    strResult += "发送失败:" + err;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return strResult;
        }
    
    }
    

      

  • 相关阅读:
    hdu 4496 D-City 并查集
    hdu 4493 Tutor 水题
    codeforces 377A. Puzzles 水题
    hdu 1257 小希的迷宫 并查集
    图论500题
    cdoj 93 King's Sanctuary 傻逼几何题
    cdoj 题目简单分类
    cdoj 80 Cube 水题
    cdoj 71 I am Lord Voldemort 水题
    cdoj 65 CD Making 水题
  • 原文地址:https://www.cnblogs.com/haorun/p/6513561.html
Copyright © 2011-2022 走看看