第三方接口该如何调用

众所周知,通过ajax调用第三方接口通常会出现跨域的问题,这是浏览器基于同源策略的保护机制。对于跨域问题,有很多解决的办法,网上有一大堆,但都是基于同一个应用系统,或者前后台虽然分离,但其前后台的程序组成一个工程,也就是说跨域问题通常需要改后台代码,而第三方接口没有提供后台代码,所以无法使用常用的方法解决跨域问题。通过近半天的探索:得出以下两种比较常见的调用接口的方法,一种使用的是form表单,另一种使用Java
(1)form表单提交(此方法在Android端不适用),form的提交不存的跨域的问题,所以可以考虑使用form的action方法来解决。

<!DOCTYPE html>
<html>
<script src="jquery.js"></script>
<body>

<form action="http://10.24.19.64:8081/DataCenter/service/hadoop/hdfs/download" method="get">
First name:<br>
<button type="button" id="btn2">下载一个zip(方法2)</button>
</form> 

<p>如果您点击提交,表单数据会被发送到名为 demo_form.asp 的页面。</p>
<script>
     $("#btn2").click(function(){
    var $eleForm = $('<form method="get" action="http://10.24.19.64:8081/DataCenter/service/hadoop/hdfs/download" >'
                            +'<input type="text" name="src" value="rootdir/hxywxtData/文件名中文乱码3.docx">'
                             +'<input type="text" name="user" value="hxadmin">'
                        +'</form>');
    $(document.body).append($eleForm);
    //提交表单,实现下载
    $eleForm.submit();
});
</script>
</body>
</html>

(2)通过Java代码调用(Android端适用)


    public void  downLoad(JSONObject jsonObj) throws JSONException {
        String localFileName = jsonObj.getString("filepaths");
        String target  =  jsonObj.getString("target");
        String urlPath = "http://10.24.19.64:8081/DataCenter/service/hadoop/hdfs/download?src=rootdir/hxywxtData/"+localFileName+"&user=hxadmin";
        String httpRes = null;
        String callback = jsonObj.getString("callback");
        try {
            URL url = new URL(urlPath);
            try {
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestProperty("accept", "*/*");
                conn.setRequestProperty("connection", "Keep-Alive");
                conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                InputStream is = conn.getInputStream();
                String cacheDir = context.getExternalCacheDir().getPath();
                String full = cacheDir+"/"+target;
                //构造一个字符流缓存
                File file  = new File(full);
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                String str = "";
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                        fos.write(buffer,0,len);
                }
                FileInputStream fis = new FileInputStream(full);

                while( fis.read(buffer)  > 0){
                   String strBuff =  new String(buffer).substring(0,5);
                    if (strBuff.equals("false"))
                    {
                            webviewpost("0",callback);
                            return;
                    }
                }
                //关闭流
                is.close();
                //断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
                //固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。
                conn.disconnect();
                webviewpost(full,callback);
               // System.out.println("完整结束");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

对于下载还可以使用window.open()方法,当然这个应该只能用在PC端。

猜你喜欢

转载自blog.csdn.net/helloworlddm/article/details/80521505
今日推荐