java 模拟http请求(跨域解决方案)

1.需要引入的jar包

    <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>

2.具体写法

//发送数据
    @RequestMapping(value="/postJCList",produces="application/json;charset=utf-8")
    @ResponseBody
    public String postJCList(@RequestParam(value="mydata") String mydata,@RequestParam(value="url") String url){
        JSONObject jsStr = JSONObject.fromObject(mydata);       
         HttpClient httpClient = new DefaultHttpClient();
            HttpPost method = new HttpPost(url); 
           method.setHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");           
           int status = 0;
            String body = null;

            if (method != null) {
                try {                  

                    //添加参数
                      JSONObject jsob=new JSONObject();
                      jsob.put("ceshi", "123");
                       StringEntity entity= new StringEntity(jsStr.toString());                  
                       entity.setContentEncoding("UTF-8");
                       entity.setContentType("application/json");

                       method.setEntity(entity);
                    long startTime = System.currentTimeMillis();

                    HttpResponse response = httpClient.execute(method);

                    System.out.println("the http method is:" + method.getEntity());
                    long endTime = System.currentTimeMillis();
                    int statusCode = response.getStatusLine().getStatusCode();
                    System.out.println("状态码:" + statusCode);
                    System.out.println("调用API 花费时间(单位:毫秒):" + (endTime - startTime));                                      
                    if (statusCode != HttpStatus.SC_OK) {
                        System.out.println("请求失败:" + response.getStatusLine());
                        status = 1;
                    }

                    //Read the response body
                    body = EntityUtils.toString(response.getEntity(), "UTF-8");
                    System.out.println(body);

                } catch (IOException e) {
                    //发生网络异常
                    System.out.println("exception occurred!\n" + ExceptionUtils.getFullStackTrace(e));
                    //网络错误
                    status = 3;
                } finally {
                    System.out.println("调用接口状态:" + status);
                }

            }
           return body;
    }   
    // 构建唯一会话Id
    public static String getSessionId(){
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        return str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值