码迷,mamicode.com
首页 > 编程语言 > 详细

java调用webserviceUrl

时间:2016-09-07 19:17:13      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

代码:

package com.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

/**
 * java访问webserviceUrl
 */
public class WebserviceUrlAccess {

    // login后保存返回的cookie,供下次连接时使用
    private String responseCookie;

    /**
     * 根据用户名密码登录远程webserviceUrl
     */
    public String login(String url, String jsonParam) {

        // 声明返回结果
        String result = "";

        BufferedReader reader = null;
        DataOutputStream out = null;

        try {
            URL postUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST"); // 设置访问类型
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/json"); // 设置参数类型
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.connect();

            out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(jsonParam);
            out.flush();
            out.close();

            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));// 设置返回数据编码
            responseCookie = connection.getHeaderField("Set-Cookie");// 取到所用的Cookie

            // 读取返回结果
            String line;
            while ((line = reader.readLine()) != null) {
                result += line;
            }

            reader.close();
            connection.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    /**
     * 登录远程webserviceUrl后,进行查询操作
     */
    public String searchData(String url, String jsonParam) {
        // 声明返回结果
        String result = "";

        BufferedReader reader = null;
        DataOutputStream out = null;

        try {
            URL postUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
            connection.setRequestProperty("Cookie", responseCookie); // 设置login时返回的cookie
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.connect();

            out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(jsonParam);
            out.flush();
            out.close();

            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));// 设置返回数据编码

            String line;
            while ((line = reader.readLine()) != null) {
                result += line;
            }

            reader.close();
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    /**
     * 使用示例
     */
    public static void main(String[] args) {

        WebserviceUrlAccess access = new WebserviceUrlAccess();

        // login
        String loginUrl = "webserviceUrl地址";
        Map<String, Object> loginParam = new HashMap<String, Object>();
        loginParam.put("user", "登录名"); // 需根据接口实际需要的参数而定
        loginParam.put("password", "登录密码");// 需根据接口实际需要的参数而定
        String loginJsonParam = JSONObject.fromObject(loginParam).toString();
        String resultLogin = access.login(loginUrl, loginJsonParam);

        // searchData
        String searchUrl = "webserviceUrl地址";
        Map<String, Object> searchParam = new HashMap<String, Object>();
        searchParam.put("fileds", "属性名");// 需根据接口实际需要的参数而定
        searchParam.put("date", "日期");// 需根据接口实际需要的参数而定
        String searchJsonParam = JSONObject.fromObject(searchParam).toString();
        String resultSearch = access.searchData(searchUrl, searchJsonParam);
    }
}

 

java调用webserviceUrl

标签:

原文地址:http://www.cnblogs.com/zj0208/p/5850266.html

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