久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP CURL與java http使用方法詳解

這篇文章主要為大家詳細介紹了PHP CURL與java http使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

php curl

有時候我們的項目需要與第三方平臺進行交互。舉個例子。

現在有A、B兩個平臺。 甲方在最初一段時間由A實現了一部分關鍵業務(如用戶信息等)。 然后基于一部分原因,現在有一些業務需要B來實現,且實現程序調用了一些敏感的接口只能在B方服務器上跑,那么只能做兩個平臺之間的交互了。curl 就是這種問題的解決方案。

curl 是一個php擴展,你可以看作一個可以訪問其他網站的精簡版瀏覽器。
要使用curl 你得在php.ini 中開啟相關的配置才能使用。
常用的平臺之間交互的數據格式 有json、xml等比較流行的數據格式。


<?php
 @param
 $url  接口地址
 $https 是否是一個Https 請求
 $post 是否是post 請求
 $post_data post 提交數據 數組格式
function curlHttp($url,$https = false,$post = false,$post_data = array())
{
  $ch = curl_init();                            //初始化一個curl
  curl_setopt($ch, CURLOPT_URL,$url);     //設置接口地址 如:http://wwww.xxxx.co/api.php
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//是否把CRUL獲取的內容賦值到變量
  curl_setopt($ch,CURLOPT_HEADER,0);//是否需要響應頭
  /*是否post提交數據*/
  if($post){
    curl_setopt($ch,CURLOPT_POST,1);
    if(!empty($post_data)){
      curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
    }
  }
  /*是否需要安全證書*/
  if($https)
  {
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // https請求 不驗證證書和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  }
  $output = curl_exec($ch);
  curl_close($ch);
  return $output;
}
?> 

現在 接口地址 http://www.xxxxx.com/api/{sid} 這個接口地址通過get 方式可以返回一個user 的 json數據格式 ,那么我們怎么去獲取第三方平臺的數據


<?php
    $sid = 1;
    $url = "http://www.xxxxx.com/api/{$sid}";
    $data = curlHttp($url);
  $user = json_decode($data,true); 
?>

其中$user就是獲取user數組信息。
在這里 curl 模擬瀏覽器對該域名進行了get請求(當然,根據我們在參數中的設置,我們也可以去模擬post https 等請求),獲取到了響應的數據。

java http 實現了類似php curl 的功能

java 是一門完全面向對象的語言,我覺得除了對象名夠長不容易記憶外。其它的都很好,且它是先編譯成字節碼然后由java虛擬機去運行的,不像 php 每次都需要去編譯一次以后采取運行。
java對php curl 的實現

文件 tool.HttpRequest


package tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import java.net.URLEncoder;

import Log.Log;

public class HttpRequest 
{
  /**
   * 向指定URL發送GET方法的請求
   * 
   * @param url
   *      發送請求的URL
   * @param param
   *      請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
   * @return String 所代表遠程資源的響應結果
   */
  public static String get(String url,String param)
  {
    String result = "";
    BufferedReader in = null;
    try {
      String urlNameString = null;

      if(param == null)
        urlNameString = url;
      else
        urlNameString = url + "?" + param;

      //System.out.println("curl http url : " + urlNameString);

      URL realUrl = new URL(urlNameString);
      // 打開和URL之間的連接
      URLConnection connection = realUrl.openConnection();
      // 設置通用的請求屬性
      connection.setRequestProperty("accept", "*/*");
      connection.setRequestProperty("connection","close");
      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.equals("") ? null : result;
  }

  /**
   * 向指定 URL 發送POST方法的請求
   * 
   * @param url
   *      發送請求的 URL
   * @param param
   *      請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
   * @return String 所代表遠程資源的響應結果
   */
  public static String post(String url, String param) {
    PrintWriter out = null;
    BufferedReader in = null;
    String result = "";
    try {
      URL realUrl = new URL(url);
      // 打開和URL之間的連接
      URLConnection conn = realUrl.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)");
      // 發送POST請求必須設置如下兩行
      conn.setDoOutput(true);
      conn.setDoInput(true);
      // 獲取URLConnection對象對應的輸出流
      out = new PrintWriter(conn.getOutputStream());
      // 發送請求參數
      out.print(param);
      // flush輸出流的緩沖
      out.flush();
      // 定義BufferedReader輸入流來讀取URL的響應
      in = new BufferedReader(
          new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        result += line;
      }
    } catch (Exception e) {
      System.out.println("發送 POST 請求出現異常!"+e);
      e.printStackTrace();
    }
    //使用finally塊來關閉輸出流、輸入流
    finally{
      try{
        if(out!=null){
          out.close();
        }
        if(in!=null){
          in.close();
        }
      }
      catch(IOException ex){
        ex.printStackTrace();
      }
    }
    return result;
  }  
}

然后類似php的使用如下

web.app.controller.IndexController


package web.app.controller;

import tool.HttpRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("Index")
public class IndexController
{
    @RequestMapping(value="index",method={RequestMethod.GET,RequestMethod.POST},produces="text/html;charset=utf-8")
     @ResponseBody
  public String index()
  {
    String sid = "1";
    String apiUrl = "http://www.xxxxx.com/api/" +sid;
        String data = HttpRequest.get(apiUrl,null);   //開始模擬瀏覽器請求
        JSONObject json = JSONObject.fromObject(data);  //解析返回的json數據結果

  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持編程學習網。

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

這篇文章主要介紹了PHP實現的防止跨站和xss攻擊代碼,是一款來自阿里云的防注入腳本,可實現針對注入、XSS攻擊等的過濾功能,需要的朋友可以參考下
這篇文章主要介紹了php實現的AES加密類定義與用法,結合完整實例形式分析了基于php的AES加密類實現及使用方法,需要的朋友可以參考下
這篇文章主要介紹了php 判斷IP為有效IP地址的方法,需要的朋友可以參考下
這篇文章主要介紹了PHP設計模式之注冊樹模式,結合實例形式詳細分析了注冊樹模式的概念、原理、實現方法與相關注意事項,需要的朋友可以參考下
這篇文章主要為大家詳細介紹了PHP微信開發之微信錄音臨時轉永久存儲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章主要介紹了php代碼實現mysql連接池效果,需要的朋友可以參考下
主站蜘蛛池模板: 国产一区二区三区四区hd | 日韩成人免费视频 | 天堂一区二区三区四区 | 亚洲一区二区黄 | 一区影视 | 欧美激情综合 | 国产91av视频在线观看 | 日韩理论电影在线观看 | 黑人粗黑大躁护士 | 欧美 中文字幕 | 国产极品粉嫩美女呻吟在线看人 | 国产免费播放视频 | 国产三区在线观看视频 | 国产亚洲精品综合一区 | 国产精品久久7777777 | 大学生a级毛片免费视频 | 国产成人精品免费视频大全最热 | 在线āv视频 | 成人免费一区二区三区视频网站 | 99爱在线| 午夜视频在线观看网站 | 中文字幕亚洲视频 | 深夜福利影院 | 久久久久国产一区二区三区 | 成人福利 | 五月天婷婷丁香 | 欧美精品久久久久久久久久 | 亚洲国产精品成人综合久久久 | 久久高清免费视频 | 国产精品一区二区免费 | 久久99精品国产 | 精品一区二区视频 | 一久久久| 一区精品在线观看 | 欧美精品99 | 欧美精品一区二区三 | 国产精品久久网 | av网站免费 | 日韩中文一区二区三区 | 成人免费三级电影 | 久久精品99国产精品 |