使用httpclient实现http接口调用实例[通俗易懂]

使用httpclient实现http接口调用实例[通俗易懂]使用httpclient实现http接口调用实例假设服务接口如下:接口地址:http://192.168.0.1/service/sendsms请求方式:post需要传递参数:c={“uid”:”10000″,”title”:”testatitle”,”content”:”thisisatest”}参数内容为json格式输出:{result:0,cod

大家好,又见面了,我是你们的朋友全栈君。

使用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

 

package com.yanek.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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;

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://192.168.0.1/service/sendsms";
		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", getStringFromJson(jobj)));
				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());
					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 (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return strResult;
	}

	
	private static String getStringFromJson(JSONObject adata) {
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		for(Object key:adata.keySet()){
			sb.append("\""+key+"\":\""+adata.get(key)+"\",");
		}
		String rtn = sb.toString().substring(0, sb.toString().length()-1)+"}";
		return rtn;
	}
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/142313.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号