起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1707|回复: 1

[分享] 调用别人的接口

[复制链接]

31

主题

54

帖子

336

积分

中级会员

Rank: 3Rank: 3

积分
336
QQ
发表于 2014-9-17 23:30:11 | 显示全部楼层 |阅读模式
本帖最后由 bingbing4647 于 2014-9-29 13:14 编辑


String result =doPost(props.getProperty("doPost").trim(), param, "utf-8", true);


public static String doPost(String url, Map<String, String> params, String charset, boolean pretty) throws Exception {
        StringBuffer response=new StringBuffer();
        HttpClient client=new HttpClient();
        PostMethod method=new PostMethod(url);
        // 设置Http Post数据
        if(params != null) {
            NameValuePair[] data= new NameValuePair[params.size()];
            int i=0;
            for(Map.Entry<String, String> entry: params.entrySet()) {
                data=new NameValuePair(entry.getKey(), entry.getValue());
                i++;
            }

            method.setRequestBody(data);
        }
        try {
            client.executeMethod(method);
            if(method.getStatusCode() == HttpStatus.SC_OK) {
                BufferedReader reader=new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
                String line;
                while((line=reader.readLine()) != null) {
                    if(pretty)
                        response.append(line).append(System.getProperty("line.separator"));
                    else
                        response.append(line);
                }
                reader.close();
            }
        } catch(IOException e) {
                e.printStackTrace();
        } catch (Exception e){
               
        }finally {
            method.releaseConnection();
        }
        return response.toString();
    }




-------------------------------------------------------------
-------------------------------------------------------------

public static String sendGet(String url) {
                String result="";
                try {
                    String urlName=url;
                    URL U=new URL(urlName);
                    URLConnection connection=U.openConnection();
                    connection.setRequestProperty("Connection", "close");
                    connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
                    connection.connect();
                    BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String line;
                    while((line=in.readLine()) != null) {
                        result+=line;
                    }
                    in.close();
                } catch(Exception e) {
                    System.out.println(e);
                }
                return result;
            }
       


58

主题

210

帖子

639

积分

高级会员

Rank: 4

积分
639
QQ
发表于 2014-11-7 16:19:31 | 显示全部楼层
使用sendGet方式成功~~~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-5-18 05:26 , Processed in 0.065474 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表