HttpClient 简单使用

如果是ssm结构的在controller层直接接受就行。

//接受请求
@controller
public void postInfo(@Valid String poCode , @Valid String suCode , @Valid String noId , @Valid String juryName){
// 简单输出一下 得到的数据 打印。
    System.out.println(poCode+suCode+noId+juryName);
}
public class duijie {


    public boolean doGet(String url , Map<String,String> map){

        HttpClient httpClient = new DefaultHttpClient();

        if(map == null){
            return false ;
        }

        List<NameValuePair> para = new ArrayList<>();
        for (Entry<String, String> header : map.entrySet()) {
             para.add(new BasicNameValuePair(header.getKey(), header.getValue()));
        }

        String str = null;
        try {
            str = EntityUtils.toString(new UrlEncodedFormEntity(para));
        } catch (ParseException | IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        HttpGet httpGet = new HttpGet(url+"?"+str);

        try {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url+"?"+str);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }  

        //httpGet.setHeader("referer", u);

        /*try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int status = httpResponse.getStatusLine().getStatusCode();
            if(HttpStatus.SC_OK == status){
                return true;
            }else{
                return false;
            } 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        return true;



    }


    public static void main(String[] args) {
        duijie dj = new duijie();
        String ipAndPort = "192.168.1.174:8080";
        String url = "http://192.168.1.30:8080/ShowBL/new";
        Map<String,String> map = new HashMap<>();
        map.put("poCode", "132303");
        map.put("suCode", "51fb3f99-e4e3-4867-8d66-206ba63b4e4b");
        map.put("noId", "5c0f0940-5dda-473b-adc1-f88f33983af1");
        map.put("juryName", "Paul");
        dj.doGet(url,map);

    }

}

猜你喜欢

转载自blog.csdn.net/liguangix/article/details/80707005