您的位置:首页 > 其它

调用接口获取接口返回信息的几种工具方法

2013-07-10 11:18 507 查看
返回值为String类型

/**

     *

     * @param urlStr 接口地址

     * @param paras 请求参数

     * @param type  调接口传参类型

     * @return

     * @throws Exception

     */

public static String invokeFareInterface(String urlStr, String paras, String type) throws Exception{

        OutputStream os = null;

        StringBuffer buffer = null;

        InputStreamReader reader = null;

//        GZIPInputStream in = null;

        try {

            int c = 0;

            buffer = new StringBuffer();

            if(urlStr!=null && !"".equals(urlStr)){

                URL url = new URL(urlStr);

                HttpURLConnection con = (HttpURLConnection) url.openConnection();

                con.setReadTimeout(60000); // 15s超时

                con.setDoOutput(true);

                if(type!=null && "post".equals(type)){    

                    con.setRequestMethod("POST");

                    con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

                }else{}

                os = con.getOutputStream();

                os.write(paras.getBytes());

                os.flush();

                os.close();

                //in = new GZIPInputStream(con.getInputStream());

                int code = con.getResponseCode();            

                if (code == 200) { // 返回成功

                    reader = new InputStreamReader(con.getInputStream(), "UTF-8");

                    while ((c = reader.read()) != -1) {

                        buffer.append((char) c);

                    }

                } else { // 访问失败

                    throw new Exception("Error occur when try to visit the url:" +

                            url.getPath() + " using HttpURLConnection");

                }

                url = null;

                con = null;

            }else{

                //exceptionlog.warn("Invoke 运价接口url为空");

            }

        } catch (Exception e) {

            //exceptionlog.error(this.getClass()+"invoke 运价接口异常:"+e.getMessage());

//            System.out.println(this.getClass()+"invoke 运价接口异常:"+e.getMessage());

            throw new Exception("Error occur execute " +

                    "HttpRemoteProxy.performImpl(), the caused by " + e);

        } finally {

            if (reader != null) {

                reader.close();

            }

            if (os != null) {

                os.close();

            }

//            if (in != null) {

//                in.close();

//            }

        }

        return buffer.toString();

    }    

使用方法调用时应注意如果为get方法调用,应当将参数也放在urlStr里边,param传一个空字符串即可,否则会有错误,底边几个调用方法与此一样

返回值为String

public String getResult(URL url,String sendMsg,String type){

        String myResponse = null;

        try {

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            if("1".equals(type)){

                conn.setRequestMethod("POST");

            }else if("2".equals(type)){

                conn.setRequestMethod("GET");

            }    

               conn.setRequestProperty("Proxy-Connection", "Keep-Alive");

               conn.setDoOutput(true);

               conn.setDoInput(true);

               OutputStream os = conn.getOutputStream();

               DataOutputStream dos = new DataOutputStream(os);

               dos.write(sendMsg.getBytes("UTF-8"));

               dos.flush();

               dos.close();

               os.close();

               InputStream is = conn.getInputStream();

               DataInputStream dis = new DataInputStream(is);

               byte[] receivedBytes = new byte[dis.available()];

               dis.read(receivedBytes);

               myResponse = new String(receivedBytes);

               System.out.println(myResponse);

               dis.close();

               is.close();

               conn.disconnect();

        }catch (Exception e) {

            e.printStackTrace();

        }

        return myResponse;

    }

返回值为String

public static String changeUrlStr(URL url, String sendMsg) {

        StringBuffer buffer = null;

        InputStreamReader reader = null;            

        InputStream in = null;

        OutputStreamWriter out = null;

        URLConnection conn = null;

        try {

            int c=0;

            conn = url.openConnection();

            conn.setDoOutput(true);// 设置输出流

            conn.setConnectTimeout(30*1000);

            conn.setReadTimeout(30*1000);

            out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");// 得到输出流

            out.write(sendMsg);// 输出post数据

            out.flush();

            out.close();

            conn.connect();

            //in = conn.getInputStream();// 得到返回的输入流

            buffer = new StringBuffer();

            reader = new InputStreamReader(conn.getInputStream(), "UTF-8");

            while ((c = reader.read()) != -1) {

                buffer.append((char) c);

            }

            return buffer.toString();

        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return null;

    }

返回值为java.io.InputStream类型

/**

*url为接口地址 sendMsg为参数

*/

public static InputStream changeUrl(URL url, String sendMsg) {

        try {

            InputStream in = null;

            OutputStreamWriter out = null;

            URLConnection conn = null;

            conn = url.openConnection();

            conn.setDoOutput(true);// 设置输出流

            conn.setConnectTimeout(30*1000);

            conn.setReadTimeout(30*1000);

            out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");// 得到输出流

            out.write(sendMsg);// 输出post数据

            out.flush();

            out.close();

            conn.connect();

            in = c
4000
onn.getInputStream();// 得到返回的输入流

            return in;

        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return null;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: