1. 程式人生 > >java HTTP get post請求,獲取二進位制檔案實現

java HTTP get post請求,獲取二進位制檔案實現

public class HttpPlugin{
 private static Log log = LogFactory.getLog(HttpPlugin.class);
 @Override
 public void init() {
  
 }
 

 public void sendGet(String url , Parameter param)
 {
  StringBuffer p=new StringBuffer();
  boolean first=true;
  for(Map.Entry<String, String> pair : param.parameters.entrySet()){
   if(first) first=false;
   else p.append('&');
   p.append(pair.getKey());
   p.append('=');
   p.append(pair.getValue());
  }
  try
  {
   String urlName = url + "?" + p.toString();
   URL realUrl = new URL(urlName);
   URLConnection conn = realUrl.openConnection();

   conn.connect();
   //獲取所有響應頭欄位
   Map<String,List<String>> map = conn.getHeaderFields();
   for (String key : map.keySet())
   {
   System.out.println(key + "--->" + map.get(key));
   }
   
   Scanner scanner=new Scanner(conn.getInputStream(),"gb2312");//以gb2312格式接收,避免中文亂碼
   StringBuffer str=new StringBuffer();
   while(scanner.hasNextLine()) {
           str.append(scanner.nextLine());
           str.append("/n");
   }
   
   System.out.println(str.toString());
   
  }catch(Exception e){
   log.error(e.getMessage());
  }
 }
 

 public void sendPost(String url,Parameter param)
 {
  PrintWriter out = null;
  String result = "";
  try{
   URL realUrl = new URL(url);
   URLConnection conn = realUrl.openConnection();
   //傳送POST請求設定
   conn.setDoOutput(true);
   conn.setDoInput(true);

   out = new PrintWriter(conn.getOutputStream());
   
   boolean first=true;
   for(Map.Entry<String, String> pair : param.parameters.entrySet()){
    if(first) first=false;
    else out.print('&');
    out.print(pair.getKey());
    out.print('=');
    out.print(pair.getValue());
   }
   out.flush();
   
   Scanner scanner=new Scanner(conn.getInputStream(),"gb2312");//以gb2312格式接收
   StringBuffer str=new StringBuffer();
   while(scanner.hasNextLine()) {
           str.append(scanner.nextLine());
           str.append("/n");
   }
   
   System.out.println(str.toString());
   
   if (out != null){
    out.close();
   }
  }catch(Exception e){
   log.error(e.getMessage());
  }
  System.out.println(result);
 }
 
 public boolean getBinaryFile(String url){
  try{
   URL u = new URL(url);
   URLConnection uc = u.openConnection();
   String contentType = uc.getContentType();
   int contentLength = uc.getContentLength();
 
   if (contentType.startsWith("text/") || (contentLength == -1))
   {
    log.error("getBinaryFile-->not a binary file");
    return false;
   }
 
   InputStream raw = uc.getInputStream();
   InputStream in = new BufferedInputStream(raw);
   byte[] data = new byte[contentLength];
   int bytesRead = 0;
   int offset = 0;
   while (offset < contentLength)
   {
    bytesRead = in.read(data,offset,data.length-offset);
    if(bytesRead == -1) break;
    offset +=bytesRead;
   }
   in.close();
 
   if (offset != contentLength)
   {
    log.error("getBinaryFile-->Only read "+offset+" bytes;Expected "+contentLength+" bytes.");
    return false;
   }
 
   String fileName = u.getFile();
   String filePath = "D://";
   fileName = fileName.substring(fileName.lastIndexOf('/')+1);
   FileOutputStream fout = new FileOutputStream(filePath+fileName);
   fout.write(data);
   fout.flush();
   fout.close();
  }catch(Exception e){
   log.error(e.getMessage());
   return false;
  }
  return true;
 }
 
 
 public Parameter getParameter(){
  return new Parameter();
 }
 
 
 public class Parameter{
  private Map<String,String> parameters=new HashMap<String,String>();
  private Parameter(){
   parameters=new HashMap<String,String>();
  }
  public boolean addParam(String name,String value){
   if(parameters!=null){
    parameters.put(name, value);
    return true;
   }else{
    return false;
   }
  }
  public boolean delParam(String name){
   if(parameters!=null){
    if(parameters.remove(name)==null){
     return false;
    }else{
     return true;
    }
   }else{
    return false;
   }
  }
 }

}

相關推薦

java HTTP get post請求獲取二進位制檔案實現

public class HttpPlugin{ private static Log log = LogFactory.getLog(HttpPlugin.class); @Override public void init() {   }   public void se

Java發送http get/post請求調用接口/方法

strong org 釋放 github string charset 獲取url lean catch 由於項目中要用,所以找了一些資料,整理下來。 GitHub地址: https://github.com/iamyong 轉自:http://blog.csdn.n

三個例子 —JAVA發送http get/post請求調用http接口、方法

客戶 trace some php += dom 取數據 響應頭 get方法 三個例子 —JAVA發送http get/post請求,調用http接口、方法 例1:使用 HttpClient import java.io.ByteArrayInputStream; imp

Java傳送http get/post請求呼叫介面/方法

由於專案中要用,所以找了一些資料,整理下來。 GitHub地址: https://github.com/iamyong    轉自:http://blog.csdn.net/capmiachael/article/details/51833531 例1:使用 HttpCl

JAVA傳送http get/post請求呼叫http介面、方法

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; impo

java模擬httpGet/Post請求並設定ip與port代理

1、因為很多公司的內網都設有代理,瀏覽器通過ip與port上網,而java程式碼模擬http get方式同樣需要外網代理; 2、Java實現http的Get/Post請求程式碼; 3、主要是設定HttpURLConnection請求頭裡面的屬性 比如Cookie、Us

java http get post 各種請求模擬瀏覽器請求

package com.hlzt.wx.util.http; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org

Java-簡單的 傳送http get/post請求 呼叫介面/方法

例子如下:1.請求引數srequestUrl 是你要傳送的介面引數用map 存起來 sRet 是返回值 如果傳送的介面有的話就寫 然後呼叫 NetUtils.requset請求 (介面,引數,get/post(選擇一種傳送方式))String sRequestUrl ="ht

Java 模擬 HTTP Get Post 請求實現論壇自動回帖

設計思路 最近想自動發帖回帖,拿某論壇試驗了一下,發現可行,不過後續沒有再使用,免得影響論壇正常執行。 帖子連結的格式為 http://bbs.***.***.**/forum.php?mod=viewthread&tid=774210

CURL命令模擬Http Get/Post請求

curl post get在對後端程序進行測試的時候,需要進行模擬連接或者書寫測試腳本. curl是一個很棒的命令. 例如目標網站Url:127.0.0.1:8080/check_your_status?user=Summer&passwd=12345678通過Get方法請求:curl protoco

php 中使用cURL發送get/post請求上傳圖片批處理

cit gda 抓取 記錄 rem 學習 網頁 lose XML https://mp.weixin.qq.com/s/8luqMEd8xt8oJxFLLCU1XA 文章正文 cURL是利用url語法規定傳輸文件和數據的工具。php中有curl拓展,一般用來實現網絡抓取,模

spring boot 常見http get ,post請求引數處理

 在定義一個Rest介面時通常會利用GET、POST、PUT、DELETE來實現資料的增刪改查;這幾種方式有的需要傳遞引數,後臺開發人員必須對接收到的引數進行引數驗證來確保程式的健壯性 GET 一般用於查詢資料,採用明文進行傳輸,一般用來獲取一些無關使用者資訊的資料 POST

Java後臺傳送post請求並接收返回資訊

/** * 向指定的 URL傳送遠端POST方法的請求 * @param url傳送請求的 URL * @param json請求引數, * @return 所代表遠端資源的響應結果 */ public static JSONObject sendPost(String

axios服務封裝可用於任何支援axios的專案中包括react和vue都可通用。get/post請求以及併發請求。以及導航欄隨意切換測試/正式環境

任何專案,只要支援axios,那麼你只要把我現在封裝的服務整個資料夾考過去即可。這個原本是我封裝在vue裡的,但是有一天公司突然來一個緊急的H5微信分享活動的專案,我當時用react搭建(zepto+node搭建其實最好)也是為了挑戰一下自己,畢竟只有三天時間。所以當我把很多vue裡封裝的東西直

java 傳送get post請求

1、匯入jar <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId&

Java http傳送post請求

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.UR

C++ 簡單實現HTTP GET/POST 請求

轉載出處:詳情http://m.blog.csdn.net/article/details?id=16336713 HTTP(超文字傳輸協議)是一種客戶端與服務端的傳輸協議,最早用於瀏覽器和伺服器之間的通訊,後來因為其使用靈活、方便等特點,廣泛用於客戶端與服務端的通訊。文章

C++ 實現 傳送HTTP Get/Post請求

1、簡述 最近簡單看了一下關於HTTP請求方面的知識,之前一直用Qt來實現,有專門HTTP請求的QNetworkAccessManager類來處理,實現也比較簡單,這裡主要講解一下用C++程式碼來實現HTTP 的Get/Post請求。 一個HTT

C# 實現http get post async sync 上傳檔案

程式碼: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading

java忽略證書驗證(相容httphttps)進行get/post請求--使用(org.apache.httpcomponents httpclient客戶端)

這兩天的任務就是跟測試接觸,在測試過程傳送資料請求上游資訊時,報了: javax.net.ssl.SSLException: hostname in certificate didn't match 截圖: 含義就是說現在程式執行的域名,與請求的證書不一致,不匹配導致的。那麼解決