httpclient 带参数 post get

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhao__zhen/article/details/83690445

httpclient 带参数 post get

//httpclient 和httpcore的maven依赖包
 <dependencies>
	  	<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.6</version>
		</dependency>
  </dependencies>

post方法

//带参数的post方法
String getTokenUr = prop.getProperty("ys.GetTokenUrl");
//        创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();
//        创建httppost 对象
        HttpPost httpPost = new HttpPost(getTokenUr);
        //设置header信息
        //指定报文头【Content-type】、【User-Agent】
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        /**
         * 获取萤石信息
         */
        String appkey = prop.getProperty("ys.AppKey");
        String secret = prop.getProperty("ys.Secret");

        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("appKey", appkey));
        list.add(new BasicNameValuePair("appSecret", secret));
        try {
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
            httpPost.setEntity(formEntity);
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String str = EntityUtils.toString(entity, "UTF-8");
            JSONObject jsonObject = JSONObject.fromObject(str);
            prop.setProperty("ys.Token", (String) JSONObject.fromObject(jsonObject.get("data")).get("accessToken"));
            URL confUri = this.getClass().getResource("/conf.properties");
            FileOutputStream fos = new FileOutputStream(new File(confUri.toURI()));
            prop.store(fos, "new token");
            response.close();
        } catch (UnsupportedEncodingException e) {
            logger.log(-1, e.getMessage());
        } catch (ClientProtocolException e) {
            logger.log(-1, e.getMessage());
        } catch (IOException e) {
            logger.log(-1, e.getMessage());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

get方法


	// 获取http客户端
		CloseableHttpClient client = HttpClients.createDefault();
		// 通过httpget方式来实现我们的get请求
		HttpGet httpGet = new HttpGet("YOUR URL");
		// 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了
		CloseableHttpResponse Response = client.execute(httpGet);
		// HttpEntity
		// 是一个中间的桥梁,在httpClient里面,是连接我们的请求与响应的一个中间桥梁,所有的请求参数都是通过HttpEntity携带过去的
		// 所有的响应的数据,也全部都是封装在HttpEntity里面
		HttpEntity entity = Response.getEntity();
		// 通过EntityUtils 来将我们的数据转换成字符串
		String str = EntityUtils.toString(entity, "UTF-8");
		// EntityUtils.toString(entity)
		System.out.println(str);
		// 关闭
		Response.close();

常见错误说明

Exception in thread “main” java.lang.NoSuchFieldError: INSTANCE
当出现这种信息的时候是因为项目里有两个httpClient.jar包,编译器默认使用版本低的包,在获取连接的时候发现这个错误,检查jar包的时候才发现,在lib目录下有一个httpClient4.0的包,将其删除即可。

猜你喜欢

转载自blog.csdn.net/zhao__zhen/article/details/83690445
今日推荐