溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java 發(fā)送http和https請(qǐng)求的實(shí)例

發(fā)布時(shí)間:2020-10-01 17:54:43 來(lái)源:腳本之家 閱讀:462 作者:一包大豫竹 欄目:編程語(yǔ)言

HTTP請(qǐng)求:

如果需要Json格式的自己轉(zhuǎn)下,度娘上N種姿勢(shì)…

//處理http請(qǐng)求 requestUrl為請(qǐng)求地址 requestMethod請(qǐng)求方式,值為"GET"或"POST" 
  public static String httpRequest(String requestUrl,String requestMethod,String outputStr){ 
    StringBuffer buffer=null; 
    try{ 
    URL url=new URL(requestUrl); 
    HttpURLConnection conn=(HttpURLConnection)url.openConnection(); 
    conn.setDoOutput(true); 
    conn.setDoInput(true); 
    conn.setRequestMethod(requestMethod); 
    conn.connect(); 
    //往服務(wù)器端寫(xiě)內(nèi)容 也就是發(fā)起http請(qǐng)求需要帶的參數(shù) 
    if(null!=outputStr){ 
      OutputStream os=conn.getOutputStream(); 
      os.write(outputStr.getBytes("utf-8")); 
      os.close(); 
    } 
    //讀取服務(wù)器端返回的內(nèi)容 
    InputStream is=conn.getInputStream(); 
    InputStreamReader isr=new InputStreamReader(is,"utf-8"); 
    BufferedReader br=new BufferedReader(isr); 
    buffer=new StringBuffer(); 
    String line=null; 
    while((line=br.readLine())!=null){ 
      buffer.append(line); 
    } 
    }catch(Exception e){ 
      e.printStackTrace(); 
    } 
    return buffer.toString(); 
  } 

HTTPS請(qǐng)求:

1、https和http的區(qū)別我這里就不介紹了,在java 中訪問(wèn)https鏈接的話需要有相應(yīng)的SSL證書(shū),如果沒(méi)有就無(wú)法訪問(wèn)(使用http訪問(wèn)https會(huì)返回403),所以我們要先自定義一個(gè)信任管理器。實(shí)現(xiàn)java自帶的X509TrustManger接口,代碼:

import java.security.cert.CertificateException; 
import java.security.cert.X509Certificate; 
import javax.net.ssl.X509TrustManager; 
public class MyX509TrustManager implements X509TrustManager { 
  @Override 
  public void checkClientTrusted(X509Certificate[] chain, String authType) 
      throws CertificateException { 
    // TODO Auto-generated method stub 
  } 
  @Override 
  public void checkServerTrusted(X509Certificate[] chain, String authType) 
      throws CertificateException { 
    // TODO Auto-generated method stub 
  } 
  @Override 
  public X509Certificate[] getAcceptedIssuers() { 
    // TODO Auto-generated method stub 
    return null; 
  } 
} 

2、然后我們就可以使用https請(qǐng)求了:

/* 
 * 處理https GET/POST請(qǐng)求 
 * 請(qǐng)求地址、請(qǐng)求方法、參數(shù) 
 * */ 
public static String httpsRequest(String requestUrl,String requestMethod,String outputStr){ 
  StringBuffer buffer=null; 
  try{ 
  //創(chuàng)建SSLContext 
  SSLContext sslContext=SSLContext.getInstance("SSL"); 
  TrustManager[] tm={new MyX509TrustManager()}; 
  //初始化 
  sslContext.init(null, tm, new java.security.SecureRandom());; 
  //獲取SSLSocketFactory對(duì)象 
  SSLSocketFactory ssf=sslContext.getSocketFactory(); 
  URL url=new URL(requestUrl); 
  HttpsURLConnection conn=(HttpsURLConnection)url.openConnection(); 
  conn.setDoOutput(true); 
  conn.setDoInput(true); 
  conn.setUseCaches(false); 
  conn.setRequestMethod(requestMethod); 
  //設(shè)置當(dāng)前實(shí)例使用的SSLSoctetFactory 
  conn.setSSLSocketFactory(ssf); 
  conn.connect(); 
  //往服務(wù)器端寫(xiě)內(nèi)容 
  if(null!=outputStr){ 
    OutputStream os=conn.getOutputStream(); 
    os.write(outputStr.getBytes("utf-8")); 
    os.close(); 
  } 
  //讀取服務(wù)器端返回的內(nèi)容 
  InputStream is=conn.getInputStream(); 
  InputStreamReader isr=new InputStreamReader(is,"utf-8"); 
  BufferedReader br=new BufferedReader(isr); 
  buffer=new StringBuffer(); 
  String line=null; 
  while((line=br.readLine())!=null){ 
    buffer.append(line); 
  } 
  }catch(Exception e){ 
    e.printStackTrace(); 
  } 
  return buffer.toString(); 
} 

以上這篇java 發(fā)送http和https請(qǐng)求的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI