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

QQ登录

只需一步,快速开始

查看: 2649|回复: 4

[分享] 后台java如何调用其他服务器上的baas服务,如何传参?

[复制链接]

28

主题

117

帖子

677

积分

高级会员

Rank: 4

积分
677
QQ
发表于 2018-6-23 19:26:05 | 显示全部楼层 |阅读模式
本帖最后由 爱我的老鼠 于 2018-6-24 12:10 编辑

1,服务器A,运行APP。
2,某个baas服务在服务器B上(也是用wex5开发的)。
3,现在服务器A,后台java,如何调用服务B上的Baas服务?

今天查了网上的例子(java调用sevlet),发现,B服务器上baas没法收到参数,需要wex5大神帮我

已经查资料解决了,方法不是最好,但能用,见下面3楼

28

主题

117

帖子

677

积分

高级会员

Rank: 4

积分
677
QQ
 楼主| 发表于 2018-6-24 08:56:49 | 显示全部楼层
本帖最后由 爱我的老鼠 于 2018-6-24 12:05 编辑

找到答案了,目前用post暂时没走通!!!换成get就可以了。下面参考没用了!!!

https://stackoverflow.com/questi ... to-get-post-request

回答:
Here is how you would add query string parameters using HttpClient 4.2 and later:

URIBuilder builder = new URIBuilder("http://example.com/");
builder.setParameter("parts", "all").setParameter("action", "finish");

HttpPost post = new HttpPost(builder.build());

The resulting URI would look like:

http://example.com/?parts=all&action=finish
回复 支持 反对

使用道具 举报

28

主题

117

帖子

677

积分

高级会员

Rank: 4

积分
677
QQ
 楼主| 发表于 2018-6-24 12:09:03 | 显示全部楼层
本帖最后由 爱我的老鼠 于 2018-6-24 12:32 编辑

比如,另外的服务器上的baas服务是:http://210.114.119.120:8090/baas/test/demo/hello。其中参数有2个分别是data1,data2。
参考java代码如下,测试后完全没问题,可以向目标服务器baas发起调用,直接copy修改就能用:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.justep.baas.action.ActionContext;

import java.io.IOException;
import java.net.URI;  
import java.net.URISyntaxException;
public class Test{
    private static CloseableHttpClient getHttpClient(){
        return HttpClients.createDefault();
    }

    private static void closeHttpClient(CloseableHttpClient client) throws IOException{
        if (client != null){
            client.close();
        }
    }
    public static JSONObject test3(JSONObject params, ActionContext context) throws URISyntaxException{
           //对照下面红色的,修改就行,注意参数部分
            URI uri = new URIBuilder()

            .setScheme("http")
            .setHost("210.114.119.120:8090")
            .setPath("/baas/test/demo/hello")
            .setParameter("data1", "wodexinxin")
            .setParameter("data2", "hello,you had passed!")
            .build();
                //创建默认的httpClient实例
                CloseableHttpClient httpClient = getHttpClient();
               
                try {
                        //用get方法发送http请求
                        //这样写是不行的!
                        //HttpGet httpget = new HttpGet("http://210.114.119.120:8090/baas/test/demo/hello?data1=niuxinxin&data2=hello,you had passed!!!");
                        HttpGet httpget = new HttpGet(uri);
                        
                        System.out.println(httpget.getURI());
                        //发送get请求
                        CloseableHttpResponse httpResponse = null;
                        httpResponse = httpClient.execute(httpget);
                        try{
                //response实体
                HttpEntity entity = httpResponse.getEntity();
                if (null != entity){
                    System.out.println("响应状态码:"+ httpResponse.getStatusLine());
                    System.out.println("-------------------------------------------------");
                    System.out.println("响应内容:" + EntityUtils.toString(entity));
                    System.out.println("-------------------------------------------------");                    
                }
            }
            finally{
                    System.out.println("-------------------------------------------------");
                    System.out.println("finally:  httpResponse.close()");
                httpResponse.close();
                System.out.println("-------------------------------------------------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
                System.out.println("-------------------------------------------------");
                System.out.println("finally:  closeHttpClient(httpClient)");
            try{
                closeHttpClient(httpClient);
            } catch (IOException e){
                e.printStackTrace();
            }
            System.out.println("-------------------------------------------------");
        }
               
                System.out.println("-------------------------------------------------");
        System.out.println("OK!!! return JSONObject!!!");
        System.out.println("-------------------------------------------------");
        //
                JSONObject ret = new JSONObject();
                ret.put("count", 1);
                ret.put("response", "");
                return ret;                  
    }         
}


回复 支持 反对

使用道具 举报

28

主题

117

帖子

677

积分

高级会员

Rank: 4

积分
677
QQ
 楼主| 发表于 2018-6-24 15:03:56 | 显示全部楼层
本帖最后由 爱我的老鼠 于 2018-6-24 15:05 编辑

//网上的代码,n多,解决方法很多。
    public static String sendGet(String url, String param) {  
        String result = "";  
        BufferedReader in = null;  
        try {  
            String urlNameString = url + "?" + param;  
            URL realUrl = new URL(urlNameString);  
            // 打开和URL之间的连接  
            URLConnection connection = realUrl.openConnection();  
            // 设置通用的请求属性  
            connection.setRequestProperty("accept", "*/*");  
            connection.setRequestProperty("connection", "Keep-Alive");  
            connection.setRequestProperty("user-agent",  
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
            // 建立实际的连接  
            connection.connect();  
            // 获取所有响应头字段  
            Map<String, List<String>> map = connection.getHeaderFields();  
            // 遍历所有的响应头字段  
            for (String key : map.keySet()) {  
                System.out.println(key + "--->" + map.get(key));  
            }  
            // 定义 BufferedReader输入流来读取URL的响应  
            in = new BufferedReader(new InputStreamReader(  
                    connection.getInputStream()));  
            String line;  
            while ((line = in.readLine()) != null) {  
                result += line;  
            }  
        } catch (Exception e) {  
            System.out.println("发送GET请求出现异常!" + e);  
            e.printStackTrace();  
        }  
        // 使用finally块来关闭输入流  
        finally {  
            try {  
                if (in != null) {  
                    in.close();  
                }  
            } catch (Exception e2) {  
                e2.printStackTrace();  
            }  
        }  
        return result;  
    }  
  
    public static JSONObject test(JSONObject params, ActionContext context){
            /*
        //发送 GET 请求
        String sr=sendGet("http://211.214.219.255:8090/baas/test/demo/hello", "data1=wdxinxin&data2=god!");
        System.out.println(sr);  
        //
                JSONObject ret = new JSONObject();
                ret.put("count", 1);
                ret.put("data", sr);
                return ret;            
    }
回复 支持 反对

使用道具 举报

28

主题

117

帖子

677

积分

高级会员

Rank: 4

积分
677
QQ
 楼主| 发表于 2018-6-24 15:04:41 | 显示全部楼层
哪位大神知道 POST 的解决方案,也贴上来吧,感激不尽!!!
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 06:10 , Processed in 0.063138 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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