1.httpclient 简单示例

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.alibaba.apitest</groupId>
  <artifactId>AutoApiTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
      <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.12</version>
    </dependency>
      
  </dependencies>
</project>
package com.httpdemo.auto;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class DoGetDemo {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        String url = "http://apis.juhe.cn/fapig/zodiac/query";
        HttpPost httpPost = new HttpPost(url);
        String key = "6d43e98f74c283db936d0e3b742ebde0";
        String keword = "";

        List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
        parameters.add(new BasicNameValuePair("key", key));
        parameters.add(new BasicNameValuePair("keyword", keword));

        httpPost.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
        HttpClient clHttp = HttpClients.createDefault();
        HttpResponse respose = clHttp.execute(httpPost);
        System.out.println(respose.getEntity());
        String result = EntityUtils.toString(respose.getEntity());
        System.out.println(result);

        System.out.println(respose.getStatusLine().getStatusCode());

    }

}

 

 GET示例

package com.httpdemo.auto;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DoGetde {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        String url = "http://apis.juhe.cn/fapig/zodiac/query";
        url += "?keyword=兔&key=6d43e98f74c283db936d0e3b742ebde0";
        CloseableHttpClient httpClients = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        HttpResponse httpResponse = httpClients.execute(get);
        String result = EntityUtils.toString(httpResponse.getEntity());
        System.out.println(result);

    }

}


三、通过json对象传输的例子

 

 

 

 

 

 

package com.httpdemo.auto;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Jsonpostdemo {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        String url = "http://xxx.xxx/api/v1/transdata";
        HttpPost post = new HttpPost(url);
        post.setHeader("x-sign", "");
        post.setHeader("Cookie",
        String jsonstr = "{\"appKey\": \"XSpace\",\"timeout\": 20000,\"v\": \"1.0\",\"api\": \"mopen.ssd.openme.ssd.bot.list.page\",\"data\": {\"pageNo\": 1\"pageSize\": 5},\"type\": \"GET\"}}";
        StringEntity stringEntity = new StringEntity(jsonstr);
        post.setEntity(stringEntity);
        HttpClient hClient = HttpClients.createDefault();
        HttpResponse httpResponse = hClient.execute(post);
        String result = EntityUtils.toString(httpResponse.getEntity());
        System.out.println(result);

    }
}

 

posted on 2022-03-10 13:58  jiapengchu  阅读(110)  评论(0)    收藏  举报

导航