java web HttpClient 开发一个接口

项目是基于SpringMvc的,最近项目中需要给另一个项目开放一个http接口供别人调数据  所以写了这么一个例子供新手参考


Controller:


package com.cicro.iris.web.infomanage;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.cicro.iris.service.infomanage.ArticleListToJsonService;
import com.cicro.iris.util.CacheManager;


/**
 * TODO 此处描写类的信息
 * 
 * @time Apr 5, 2016 3:08:07 PM
 * @author cuixx
 * @since JDK1.7
 */
@Controller
@RequestMapping(value="/sys/infomanage/ArticleListToJson")
public class ArticleListToJsonController{
    
    
    @Resource
    ArticleListToJsonService articleListToJsonService;
    
    @RequestMapping(value="getJson")
    @ResponseBody
    public String getJson(@RequestParam(value="myy_code")String myy_code,HttpServletRequest request) {
        if(CacheManager.hasCache(myy_code)) {
           return CacheManager.getCache(myy_code);
        }
        else
        {
           CacheManager.putCache(myy_code,articleListToJsonService.getJson(myy_code));
           return articleListToJsonService.getJson(myy_code);
           
        }
    }
}


Client:

package com.cicro.iris.util;


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;




/**
 * TODO 此处描写类的信息
 * 
 * @time Mar 23, 2016 5:21:16 PM
 * @author cuixx
 * @since JDK1.7
 */
public class test{  
    private static void tmep() {
      HttpClient httpClient=new DefaultHttpClient();
      String url="http://localhost:8080/iris/sys/infomanage/ArticleListToJson/getJson";//?myy_code=ZTFL-001-ZT001-LM001
      HttpPost post=new HttpPost(url);
      post.addHeader("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
      post.addHeader("Accept-Encoding", "gzip, deflate");
      post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");


      try {
       List<NameValuePair> namevaluepairs=new ArrayList<NameValuePair>();
         /* JSONObject obj=new JSONObject();
          obj.put("myy_code", "ZTFL-001-ZT001-LM001");*/
          namevaluepairs.add(new BasicNameValuePair("myy_code","ZTFL-001-ZT001-LM001"));
          post.setEntity(new UrlEncodedFormEntity(namevaluepairs,"UTF-8"));
          post.addHeader("Content-type", "application/x-www-form-urlencoded");  
          HttpResponse response=httpClient.execute(post);
          if(response.getStatusLine().getStatusCode()==200) {
              String conResult = EntityUtils.toString(response.getEntity());
              System.out.print(conResult);
          } else {  
              String err = response.getStatusLine().getStatusCode()+"";  
              System.out.print(err);
          }  
          
      }catch(Exception e) {
          e.printStackTrace();
      }
    }
    
    public static void main(String[] args) {
        tmep();
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值