码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient

时间:2015-10-08 14:33:57      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
            String url = "http://localhost:8080/Demo/user/auth";
            HttpPost post = new HttpPost(url);
            
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
            formparams.add(new BasicNameValuePair("memberid", "4"));
            formparams.add(new BasicNameValuePair("image", "resources/upload/images/916970d6-1a9a-496a-8217-9964940a10f7.jpg,resources/upload/images/aec5925f-afa0-4ef9-8fbc-c27d349a6bd4.jpg"));
            formparams.add(new BasicNameValuePair("type", "3"));
            
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); 
            post.setEntity(entity);
    
            CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(post);
    
            if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(
                        closeableHttpResponse.getEntity(), "UTF-8");
                System.out.println(result);
            }
            closeableHttpResponse.close();
            closeableHttpClient.close();

 

Member member = new Member();
            member.setMemberid(5L);
            member.setNickname("思思思");
            member.setSex(1);
            member.setHavecar(1);
            member.setCarcode("浙A4455");
            member.setCarbirthday("2015-08-12");
            member.setHoroscope("金牛座");

            String url = "http://localhost:8080/Demo/user/modifyUserInfo";
            
            ObjectMapper mapper = new ObjectMapper();
            String memberJson = mapper.writeValueAsString(member);

            CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);

            StringEntity entity = new StringEntity(memberJson, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);

            CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost);
            if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
                System.out.println(result);
            }
            closeableHttpResponse.close();
            closeableHttpClient.close();
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();

        File file1 = new File(
                "E:/carimage/big/4028b2b64c0d9715014c2c1cb88f2168_big_0.jpg");
        File file2 = new File(
                "E:/carimage/big/4028b2b64c0d9715014c26b482e419f7_big_0.jpg");
HttpPost post = new HttpPost("http://127.0.0.1:8080/Demo/user/uploadFiles");

        FileBody fileBody1 = new FileBody(file1);
        FileBody fileBody2 = new FileBody(file2);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addPart("image1", fileBody1);
        builder.addPart("image2", fileBody2);
        HttpEntity entity = builder.build();

        post.setEntity(entity);

        CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(post);

        if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
            System.out.println(result);
        }
        closeableHttpResponse.close();
        closeableHttpClient.close();

 

HttpClient

标签:

原文地址:http://www.cnblogs.com/dingyingsi/p/4860691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!