httpclient http接口调用

来源:互联网 发布:举报淘宝店铺会怎么样 编辑:程序博客网 时间:2024/03/29 13:51

使用httpclient实现http接口调用实例


假设服务接口如下:

接口地址: http://192.168.0.1/service/sendsms

请求方式: post

需要传递参数: c= {"uid":"10000","title":"test a title","content":"this is a test"}

参数内容为json格式

输出:{result:0,code:"success"} 

格式为json格式:result:1 .成功,0. 失败

code: 为提示信息


客户端调用代码:使用httpclient-4.0.1.jar

 

[java] view plaincopy
  1. package com.yanek.test;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import net.sf.json.JSONObject;  
  8.   
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.NameValuePair;  
  11. import org.apache.http.client.ClientProtocolException;  
  12. import org.apache.http.client.HttpClient;  
  13. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  14. import org.apache.http.client.methods.HttpPost;  
  15. import org.apache.http.impl.client.DefaultHttpClient;  
  16. import org.apache.http.message.BasicNameValuePair;  
  17. import org.apache.http.util.EntityUtils;  
  18.   
  19. public class TestSendSMS {  
  20.   
  21.     /** 
  22.      * @param args 
  23.      */  
  24.     public static void main(String[] args) {  
  25.       
  26.         String uid="12345678";  
  27.         String title="test";  
  28.         String content="test a content";  
  29.         String ret=sendSms(uid ,title,content);  
  30.         System.out.println(ret);  
  31.   
  32.         if(ret.indexOf("失败")<0)  
  33.         {  
  34.             System.out.println("成功发送sms");  
  35.         }  
  36.         else  
  37.         {  
  38.             System.out.println("失败发送");  
  39.         }  
  40.   
  41.     }  
  42.       
  43.       
  44.   
  45.     public static String sendSms(String uid,String title,String content){  
  46.         HttpClient httpclient = new DefaultHttpClient();  
  47.         String smsUrl="http://192.168.0.1/service/sendsms";  
  48.         HttpPost httppost = new HttpPost(smsUrl);  
  49.         String strResult = "";  
  50.           
  51.         try {  
  52.               
  53.                 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
  54.                 JSONObject jobj = new JSONObject();  
  55.                 jobj.put("uid", uid);  
  56.                 jobj.put("title", title);  
  57.                 jobj.put("content",content);  
  58.                   
  59.                   
  60.                 nameValuePairs.add(new BasicNameValuePair("msg", getStringFromJson(jobj)));  
  61.                 httppost.addHeader("Content-type""application/x-www-form-urlencoded");  
  62.                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));  
  63.                   
  64.                 HttpResponse response = httpclient.execute(httppost);  
  65.                 if (response.getStatusLine().getStatusCode() == 200) {  
  66.                     /*读返回数据*/  
  67.                     String conResult = EntityUtils.toString(response  
  68.                             .getEntity());  
  69.                     JSONObject sobj = new JSONObject();  
  70.                     sobj = sobj.fromObject(conResult);  
  71.                     String result = sobj.getString("result");  
  72.                     String code = sobj.getString("code");  
  73.                     if(result.equals("1")){  
  74.                         strResult += "发送成功";  
  75.                     }else{  
  76.                         strResult += "发送失败,"+code;  
  77.                     }  
  78.                       
  79.                 } else {  
  80.                     String err = response.getStatusLine().getStatusCode()+"";  
  81.                     strResult += "发送失败:"+err;  
  82.                 }  
  83.         } catch (ClientProtocolException e) {  
  84.             e.printStackTrace();  
  85.         } catch (IOException e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.           
  89.         return strResult;  
  90.     }  
  91.   
  92.       
  93.     private static String getStringFromJson(JSONObject adata) {  
  94.         StringBuffer sb = new StringBuffer();  
  95.         sb.append("{");  
  96.         for(Object key:adata.keySet()){  
  97.             sb.append("\""+key+"\":\""+adata.get(key)+"\",");  
  98.         }  
  99.         String rtn = sb.toString().substring(0, sb.toString().length()-1)+"}";  
  100.         return rtn;  
  101.     }  
  102. }  
0 0
原创粉丝点击