您的位置:首页 > 理论基础 > 计算机网络

java web HttpClient 开发一个接口

2016-04-14 11:01 726 查看
项目是基于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();

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java httpCient