跨域訪問http接口的使用


最近在弄一個sip網頁集成版軟電話,為了功能的完善,呼叫中心的工作人員為我們提供了一個http接口,我先在網頁中直接打開分析了一下他的返回值,然后又放到js中去訪問,結果一放到js中一訪問就發現瀏覽器會提示:

剛開始我百思不得其解,查詢過表頭中未找到源http://localhost:8080,原本我還以為是在發送的時候我的表頭有問題,但是用html5設置表頭嘗試了幾次,發現還是提示一樣的問題,於是我又在網上查詢了一下0x80070005這個錯誤代碼,網上提示說是屬於跨域了,於是我又跟着網上的方法試了一下:

方法一:get方式獲取

$.ajax({ dataType:'jsonp',  

         url:"http://10.0.0.230/asterccinterfaces?EVENT=GetCdr&orgidentity=dc&user=7501&password=7501&pwdtype=plaintext",  

    date:'get'  

             success: function(data){         alert(data)                  }  ,

  });  

對於跨域的訪問,只能使用get,並且要設置dataType,有的朋友這個樣設置之后就可以成功運行了,但是很可惜也不知道是電腦的設置問題還是什么,反正這個方法在我這里無效,於是我又繼續嘗試,突然就想到了瀏覽器的安全權限設置,於是有了方法二

方法二:瀏覽器安全站點設置

1.將訪問地址加入受信任站點:http://10.0.0.230

2.點擊自定義,將通過域訪問數據源設置為啟用

這種方法如果是固定電腦使用的話,那還是蠻方便的,但是如果大面積的客戶端訪問服務器那就不行了,畢竟我們不可能讓每一個客戶都修改自己的瀏覽器吧,更何況有的人還不會,特別是一些商業網,這是更不允許的。因此這個方法建議大家視情況而用。

方法三:后台發送請求接收返回數據:參照http://blog.csdn.net/sd0902/article/details/10097457

public class HttpInvoker {

    public static final String GET_URL = "http://10.0.0.230/asterccinterfaces";

    public static final String POST_URL = "http://10.0.0.230/asterccinterfaces";

    public static void readContentFromGet() throws IOException {
        // 拼湊get請求的URL字串,使用URLEncoder.encode對特殊和不可見字符進行編碼
        String getURL = GET_URL + "??EVENT=GetCdr&orgidentity=dc&user=7501&password=7501&pwdtype=plaintext&username="   + URLEncoder.encode("fat man""utf-8");
        URL getUrl = new URL(getURL);
        // 根據拼湊的URL,打開連接,URL.openConnection函數會根據URL的類型,
        // 返回不同的URLConnection子類的對象,這里URL是一個http,因此實際返回的是HttpURLConnection
        HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
        // 進行連接,但是實際上get request要在下一句的connection.getInputStream()函數中才會真正發到
        // 服務器
        connection.connect();
        // 取得輸入流,並使用Reader讀取
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        System.out.println("=============================");
        System.out.println("Contents of get request");
        System.out.println("=============================");
        String lines;
        while ((lines = reader.readLine()) != null{
            System.out.println(lines);
        }
        reader.close();
        // 斷開連接
        connection.disconnect();
        System.out.println("=============================");
        System.out.println("Contents of get request ends");
        System.out.println("=============================");
    }

    public static void readContentFromPost() throws IOException {
        // Post請求的url,與get不同的是不需要帶參數
        URL postUrl = new URL(POST_URL);
        // 打開連接
        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
        // Output to the connection. Default is
        // false, set to true because post
        // method must write something to the
        // connection
        // 設置是否向connection輸出,因為這個是post請求,參數要放在
        // http正文內,因此需要設為true
        connection.setDoOutput(true);
        // Read from the connection. Default is true.
        connection.setDoInput(true);
        // Set the post method. Default is GET
        connection.setRequestMethod("POST");
        // Post cannot use caches
        // Post 請求不能使用緩存
        connection.setUseCaches(false);
        // This method takes effects to
        // every instances of this class.
        // URLConnection.setFollowRedirects是static函數,作用於所有的URLConnection對象。
        // connection.setFollowRedirects(true);

        // This methods only
        // takes effacts to this
        // instance.
        // URLConnection.setInstanceFollowRedirects是成員函數,僅作用於當前函數
        connection.setInstanceFollowRedirects(true);
        // Set the content type to urlencoded,
        // because we will write
        // some URL-encoded content to the
        // connection. Settings above must be set before connect!
        // 配置本次連接的Content-type,配置為application/x-www-form-urlencoded的
        // 意思是正文是urlencoded編碼過的form參數,下面我們可以看到我們對正文內容使用URLEncoder.encode
        // 進行編碼
        connection.setRequestProperty("Content-Type""application/x-www-form-urlencoded");
        // 連接,從postUrl.openConnection()至此的配置必須要在connect之前完成,
        // 要注意的是connection.getOutputStream會隱含的進行connect。
        connection.connect();
        DataOutputStream out = new DataOutputStream(connection
                .getOutputStream());
        // The URL-encoded contend
        // 正文,正文內容其實跟get的URL中'?'后的參數字符串一致
        String content = "firstname=" + URLEncoder.encode("一個大肥人""utf-8");
        // DataOutputStream.writeBytes將字符串中的16位的unicode字符以8位的字符形式寫道流里面
        out.writeBytes(content); 

        out.flush();
        out.close(); // flush and close
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String line;
        System.out.println("=============================");
        System.out.println("Contents of post request");
        System.out.println("=============================");
        while ((line = reader.readLine()) != null{
            System.out.println(line);
        }
        System.out.println("=============================");
        System.out.println("Contents of post request ends");
        System.out.println("=============================");
        reader.close();
        connection.disconnect();
    }

     
     public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            readContentFromGet();
            readContentFromPost();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

上面的readContentFromGet()函數產生了一個get請求,傳給servlet一個username參數,值為"fat man"。 readContentFromPost()函數產生了一個post請求,傳給servlet一個firstname參數,值為"一個大肥人"。 HttpURLConnection.connect函數,實際上只是建立了一個與服務器的tcp連接,並沒有實際發送http請求。無論是post還是get,http請求實際上直到HttpURLConnection.getInputStream()這個函數里面才正式發送出去。
readContentFromPost() 中,順序是重中之重,對connection對象的一切配置(那一堆set函數)都必須要在connect()函數執行之前完成。而對  outputStream的寫操作,又必須要在inputStream的讀操作之前。這些順序實際上是由http請求的格式決定的。
http 請求實際上由兩部分組成,一個是http頭,所有關於此次http請求的配置都在http頭里面定義,一個是正文content,在connect()函 數里面,會根據HttpURLConnection對象的配置值生成http頭,因此在調用connect函數之前,就必須把所有的配置准備好。

緊接着http頭的是http請求的正文,正文的內容通過outputStream寫入,實際上outputStream不是一個網絡流,充其量是個字符串流,往里面寫入的東西不會立即發送到網絡,而是在流關閉后,根據輸入的內容生成http正文。

至 此,http請求的東西已經准備就緒。在getInputStream()函數調用的時候,就會把准備好的http請求正式發送到服務器了,然后返回一個 輸入流,用於讀取服務器對於此次http請求的返回信息。由於http請求在getInputStream的時候已經發送出去了(包括http頭和正 文),因此在getInputStream()函數之后對connection對象進行設置(對http頭的信息進行修改)或者寫入  outputStream(對正文進行修改)都是沒有意義的了,執行這些操作會導致異常的發生
上節說道,post請求的OutputStream實際上不是網絡流,而是寫入內存,在getInputStream中才真正把寫道流里面的內容作為正文 與根據之前的配置生成的http request頭合並成真正的http request,並在此時才真正向服務器發送。

HttpURLConnection.setChunkedStreamingMode 函數可以改變這個模式,設置了ChunkedStreamingMode后,不再等待OutputStream關閉后生成完整的http request一次過發送,而是先發送http request頭,正文內容則是網路流的方式實時傳送到服務器。實際上是不告訴服務器http正文的長度,這種模式適用於向服務器傳送較大的或者是不容易 獲取長度的數據,如文件。 

public static void readContentFromChunkedPost() throws IOException {         URL postUrl = new URL(POST_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/x-www-form-urlencoded");         /**//*          * 與readContentFromPost()最大的不同,設置了塊大小為5字節          */         connection.setChunkedStreamingMode(5);         connection.connect();         /**//*          * 注意,下面的getOutputStream函數工作方式於在readContentFromPost()里面的不同          * 在readContentFromPost()里面該函數仍在准備http request,沒有向服務器發送任何數據          * 而在這里由於設置了ChunkedStreamingMode,getOutputStream函數會根據connect之前的配置          * 生成http request頭,先發送到服務器。          */         DataOutputStream out = new DataOutputStream(connection                 .getOutputStream());         String content = "firstname=" + URLEncoder.encode("一個大肥人                                                                               " +                 "                                          " +                 "asdfasfdasfasdfaasdfasdfasdfdasfs""utf-8");         out.writeBytes(content); 
        out.flush();         out.close(); // 到此時服務器已經收到了完整的http request了,而在readContentFromPost()函數里,要等到下一句服務器才能收到http請求。         BufferedReader reader = new BufferedReader(new InputStreamReader(                 connection.getInputStream()));                  out.flush();         out.close(); // flush and close         String line;         System.out.println("=============================");         System.out.println("Contents of post request");         System.out.println("=============================");         while ((line = reader.readLine()) != null{             System.out.println(line);         }         System.out.println("=============================");         System.out.println("Contents of post request ends");         System.out.println("=============================");         reader.close();         connection.disconnect();     }

 

這種方法雖然 說在服務器端書寫是有點麻煩,但是安全性和客戶使用的方便性都是目前我所知道的所有方法中最好使用的一個,當然以后有時間的話還可以考慮一下以下方法:

方法一:

在你該域名下的服務器程序下面,直接模擬瀏覽器獲取到這個值,在用ajax訪問該域名下面的就可以curl

方法二:

看有其它類庫有沒方法直接訪問這個http地址,拿到這個值你就可以在你本地做了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM