HTTP請求工具類

来源:http://www.cnblogs.com/zhangpengnike/archive/2016/02/19/5200813.html
-Advertisement-
Play Games

HTTP請求工具類,適用於微信伺服器請求,可以自測 代碼; 1 /// <summary> 2 /// HTTP請求工具類 3 /// </summary> 4 public class HttpRequestUtil 5 { 6 #region 請求Url 7 8 #region 請求Url,不發


  HTTP請求工具類,適用於微信伺服器請求,可以自測

代碼;

  1 /// <summary>
  2     /// HTTP請求工具類
  3     /// </summary>
  4     public class HttpRequestUtil
  5     {
  6         #region 請求Url
  7 
  8         #region 請求Url,不發送數據
  9         /// <summary>
 10         /// 請求Url,不發送數據
 11         /// </summary>
 12         public static string RequestUrl(string url)
 13         {
 14             return RequestUrl(url, "POST");
 15         }
 16         #endregion
 17 
 18         #region 請求Url,不發送數據
 19         /// <summary>
 20         /// 請求Url,不發送數據
 21         /// </summary>
 22         public static string RequestUrl(string url, string method)
 23         {
 24             // 設置參數
 25             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 26             CookieContainer cookieContainer = new CookieContainer();
 27             request.CookieContainer = cookieContainer;
 28             request.AllowAutoRedirect = true;
 29             request.Method = method;
 30             request.ContentType = "text/html";
 31             request.Headers.Add("charset", "utf-8");
 32 
 33             //發送請求並獲取相應回應數據
 34             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
 35             //直到request.GetResponse()程式才開始向目標網頁發送Post請求
 36             Stream responseStream = response.GetResponseStream();
 37             StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
 38             //返回結果網頁(html)代碼
 39             string content = sr.ReadToEnd();
 40             return content;
 41         }
 42         #endregion
 43 
 44         #region 請求Url,發送數據
 45         /// <summary>
 46         /// 請求Url,發送數據
 47         /// </summary>
 48         public static string PostUrl(string url, string postData)
 49         {
 50             byte[] data = Encoding.UTF8.GetBytes(postData);
 51 
 52             // 設置參數
 53             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 54             CookieContainer cookieContainer = new CookieContainer();
 55             request.CookieContainer = cookieContainer;
 56             request.AllowAutoRedirect = true;
 57             request.Method = "POST";
 58             request.ContentType = "application/x-www-form-urlencoded";
 59             request.ContentLength = data.Length;
 60             Stream outstream = request.GetRequestStream();
 61             outstream.Write(data, 0, data.Length);
 62             outstream.Close();
 63 
 64             //發送請求並獲取相應回應數據
 65             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
 66             //直到request.GetResponse()程式才開始向目標網頁發送Post請求
 67             Stream instream = response.GetResponseStream();
 68             StreamReader sr = new StreamReader(instream, Encoding.UTF8);
 69             //返回結果網頁(html)代碼
 70             string content = sr.ReadToEnd();
 71             return content;
 72         }
 73         #endregion
 74 
 75         #endregion
 76 
 77         #region Http下載文件
 78         /// <summary>
 79         /// Http下載文件
 80         /// </summary>
 81         public static string HttpDownloadFile(string url, string path)
 82         {
 83             // 設置參數
 84             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 85 
 86             //發送請求並獲取相應回應數據
 87             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
 88             //直到request.GetResponse()程式才開始向目標網頁發送Post請求
 89             Stream responseStream = response.GetResponseStream();
 90 
 91             //創建本地文件寫入流
 92             Stream stream = new FileStream(path, FileMode.Create);
 93 
 94             byte[] bArr = new byte[1024];
 95             int size = responseStream.Read(bArr, 0, (int)bArr.Length);
 96             while (size > 0)
 97             {
 98                 stream.Write(bArr, 0, size);
 99                 size = responseStream.Read(bArr, 0, (int)bArr.Length);
100             }
101             stream.Close();
102             responseStream.Close();
103             return path;
104         }
105         #endregion
106 
107         #region Http上傳文件
108         /// <summary>
109         /// Http上傳文件
110         /// </summary>
111         public static string HttpUploadFile(string url, string path)
112         {
113             // 設置參數
114             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
115             CookieContainer cookieContainer = new CookieContainer();
116             request.CookieContainer = cookieContainer;
117             request.AllowAutoRedirect = true;
118             request.Method = "POST";
119             string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線
120             request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
121             byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
122             byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
123 
124             int pos = path.LastIndexOf("\\");
125             string fileName = path.Substring(pos + 1);
126 
127             //請求頭部信息 
128             StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
129             byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
130 
131             FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
132             byte[] bArr = new byte[fs.Length];
133             fs.Read(bArr, 0, bArr.Length);
134             fs.Close();
135 
136             Stream postStream = request.GetRequestStream();
137             postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
138             postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
139             postStream.Write(bArr, 0, bArr.Length);
140             postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
141             postStream.Close();
142 
143             //發送請求並獲取相應回應數據
144             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
145             //直到request.GetResponse()程式才開始向目標網頁發送Post請求
146             Stream instream = response.GetResponseStream();
147             StreamReader sr = new StreamReader(instream, Encoding.UTF8);
148             //返回結果網頁(html)代碼
149             string content = sr.ReadToEnd();
150             return content;
151         }
152         #endregion
153 
154     }
View Code

有什麼問題可以隨時溝通

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、sealed 修飾符 概念: C#提出了一個密封類(sealed class)的概念,幫助開發人員來解決這一問題。 密封類在聲明中使用sealed 修飾符,這樣就可以防止該類被其它類繼承。如果試圖將一個密封類作為其它類的基類,C#將提示出錯。理所當然,密封類不能同時又是抽象類,因為抽象總是希望被
  • ---恢復內容開始--- RazorEngine模板引擎大大的幫助了我們簡化字元串的拼接與方法的調用,開源之後,現在在簡單的web程式,winform程式,甚至控制台程式都可以利用它來完成。 但如何在使用中調用方法和使用自定義模板呢?來看這樣一個例子 1 string str="hello @Mod
  • 使用Visual Studio寫代碼,經常遇到的一個問題就是切換中文輸入法麻煩,輸入完註釋//,要切換到中文,輸入完引號,要輸入中文,然後還需要切換回來,有沒有? 有時候中文輸入法忽然失效有沒有?明明在中文輸入法狀態下,輸入不了中文,有沒有?
  • 話說有一天,臨近下班無心工作,在網上看各種文章,閱讀到了一篇名為《聊聊大麥網UWP版的首頁頂部圖片聯動效果的實現方法》(傳遞:http://www.cnblogs.com/hippieZhou/p/4755290.html),看到別人評論自己做的產品,頓時來了興趣,閱讀過後,hippieZhou童鞋
  • 近段時間,需要寫一個小功能,就是需要判斷程式是否已經運行。某個程式安裝後,也許被多個用戶運行。那怎樣判斷當前用戶已經運行了此程式了呢?下麵是Insus.NET的做法,就是:《VB.NET WinForm獲取運行程式用戶名》http://www.cnblogs.com/insus/p/5194839.
  • (轉)這裡給大家分享幾個VS版本,都是最終版的,也是中文版的!. Visual Studio 2005:http://pan.baidu.com/s/1c0eudyS Visual Studio 2008:http://pan.baidu.com/s/1i3GJ7pj Visual Studio 2
  • 以下非原創作品,但都是自己看過理解並寫過,記錄下來,以便之後項目的使用或其它用途。 (1)只需要簡單配置單一屬性值: 1 <configuration> 2 <configSections> 3 <!--配置讀取的全名稱--> 4 <section name="simple" type="Confi
  • 使用 HttpResponse 對象 HttpResponse 對象是與 HttpRequest 對象相對應的,用來表示構建中的響應。它當中提供了方法和屬性可供我們自定義響應,有一些在使用 MVC 視圖的時候很少使用到,但是在使用其他組件的時候可能十分有用,比如模塊是處理器。 同 HttpReque
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...