1. 程式人生 > >C# winform 作為服務端接收http請求

C# winform 作為服務端接收http請求

使用httplistener監聽來自客戶端的http請求,對於Get請求的資料可以通過Request.QueryString["引數"]獲取

而對於來自客戶端的Post請求則不能使用Request[""]獲取,需要將獲取分析請求流中的資料拿到引數

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. namespace HttpListenerPost  
  9. {  
  10.     /// <summary>
  11.     /// HttpListenner監聽Post請求引數值實體
  12.     /// </summary>
  13.     publicclass HttpListenerPostValue  
  14.     {  
  15.         /// <summary>
  16.         /// 0=> 引數
  17.         /// 1=> 檔案
  18.         /// </summary>
  19.         publicint type = 0;  
  20.         publicstring
     name;  
  21.         publicbyte[] datas;  
  22.     }  
  23.     /// <summary>
  24.     /// 獲取Post請求中的引數和值幫助類
  25.     /// </summary>
  26.     publicclass HttpListenerPostParaHelper  
  27.     {  
  28.         private HttpListenerContext request;  
  29.         public HttpListenerPostParaHelper(HttpListenerContext request)  
  30.         {  
  31.             this.request = request;  
  32.         }  
  33.         privatebool CompareBytes(byte[] source, byte[] comparison)  
  34.         {  
  35.             try
  36.             {  
  37.                 int count = source.Length;  
  38.                 if (source.Length != comparison.Length)  
  39.                     returnfalse;  
  40.                 for (int i = 0; i < count; i++)  
  41.                     if (source[i] != comparison[i])  
  42.                         returnfalse;  
  43.                 returntrue;  
  44.             }  
  45.             catch
  46.             {  
  47.                 returnfalse;  
  48.             }  
  49.         }  
  50.         privatebyte[] ReadLineAsBytes(Stream SourceStream)  
  51.         {  
  52.             var resultStream = new MemoryStream();  
  53.             while (true)  
  54.             {  
  55.                 int data = SourceStream.ReadByte();  
  56.                 resultStream.WriteByte((byte)data);  
  57.                 if (data == 10)  
  58.                     break;  
  59.             }  
  60.             resultStream.Position = 0;  
  61.             byte[] dataBytes = newbyte[resultStream.Length];  
  62.             resultStream.Read(dataBytes, 0, dataBytes.Length);  
  63.             return dataBytes;  
  64.         }  
  65.         /// <summary>
  66.         /// 獲取Post過來的引數和資料
  67.         /// </summary>
  68.         /// <returns></returns>
  69.         public List<HttpListenerPostValue> GetHttpListenerPostValue()  
  70.         {  
  71.             try
  72.             {  
  73.                 List<HttpListenerPostValue> HttpListenerPostValueList = new List<HttpListenerPostValue>();  
  74.                 if (request.Request.ContentType.Length > 20 && string.Compare(request.Request.ContentType.Substring(0, 20), "multipart/form-data;"true) == 0)  
  75.                 {  
  76.                     string[] HttpListenerPostValue = request.Request.ContentType.Split(';').Skip(1).ToArray();  
  77.                     string boundary = string.Join(";", HttpListenerPostValue).Replace("boundary=""").Trim();  
  78.                     byte[] ChunkBoundary = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");  
  79.                     byte[] EndBoundary = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");  
  80.                     Stream SourceStream = request.Request.InputStream;  
  81.                     var resultStream = new MemoryStream();  
  82.                     bool CanMoveNext = true;  
  83.                     HttpListenerPostValue data = null;  
  84.                     while (CanMoveNext)  
  85.                     {  
  86.                         byte[] currentChunk = ReadLineAsBytes(SourceStream);  
  87.                         if (!Encoding.UTF8.GetString(currentChunk).Equals("\r\n"))  
  88.                             resultStream.Write(currentChunk, 0, currentChunk.Length);  
  89.                         if (CompareBytes(ChunkBoundary, currentChunk))  
  90.                         {  
  91.                             byte[] result = new

    相關推薦

    C# winform 作為服務接收http請求

    使用httplistener監聽來自客戶端的http請求,對於Get請求的資料可以通過Request.QueryString["引數"]獲取 而對於來自客戶端的Post請求則不能使用Request[""]獲取,需要將獲取分析請求流中的資料拿到引數 using System;

    安卓使用Socket發送中文,C語言服務接收亂碼問題解決方式

    article nbsp ons size ret con pre n+1 utf8 今天用安卓通過Socket發送數據到電腦上使用C語言寫的服務端,發送英文沒有問題,可當把數據改變成中文時,服務端接收到的數據確是亂碼。 突然想到。VS的預處理使用的

    HttpURLConnection 發送PUT請求 json請求體 與服務接收

    logs esp appid 請求 edi write webtest read tco public void testHttp() { String result = ""; try {

    接收客戶HTTP請求期間發送過來的值(HttpRequestBase/HttpRequest、HttpContextBase/HttpConext、HttpContext.Current)

    實例 tro 註意 quest 內部 存儲 信息 post span 雖然基本知道Asp.Net MVC程序如何接收來自客戶端HTTP的信息,但是關於HTTP的請求流轉和數據傳遞機制有些模糊,今天就來稍作整理吧。 來看看客戶端代碼: <form action="De

    C# 建立服務http服務,可用於WebService、分散式資料庫訪問。

    宣告提供服務的主機與服務名:         private static ServiceHost _host;         private static readonly string strServiceNa

    解決.NET Web API生成的Help無Controller說明&服務接收不到請求

    今天在用.NET Web API寫一個介面的時候遇到一個問題。在Controller中新加了一個方法,客戶端就不能請求介面了,當時建WEB API專案是用的VS預設設定,在服務端打斷點一直沒有進去,而APP端一直報服務端響應失敗!奇怪的是連生成的Help API說明都沒Controller說明。

    c#5 async Task非同步Socket服務接收資料

    c#5有的新特性async非同步關鍵字配合Task使用。非同步Task將很大的提高程式效能,所以這次我來分享一段採用async關鍵字和Task的socket服務端程式碼using System; using System.Collections.Generic; using

    C#客戶傳送http請求與伺服器通訊

    本文介紹瞭如何使用C#,通過HttpWebRequest方法,向服務端傳送get,post,put和delete請求 環境介紹 軟體 vs2013 程式語言c# winform 服務端採用java+spring,restful風格 在客戶端,通過H

    C/C++使用libcurl庫發送http請求(get和post可以用於請求html信息,也可以請求xml和json等串)

    網絡連接 get 編譯 eas views vs2015 return tar linux C++要實現http網絡連接,需要借助第三方庫,libcurl使用起來還是很方便的 環境:win32 + vs2015 如果要在Linux下使用,基本同理 1,下載

    java在線聊天項目0.4版本 制作服務接收連接,客戶連接功能 新增客戶窗口打開時光標指向下邊文本域功能,使用WindowListener監聽WindowAdapter

    內部 frame visible [] one exit eve awt dap 建一個服務端類ChatServer,用於設置端口接收連接 package com.swift; import java.io.IOException; import java.net.Se

    java在線聊天項目0.9版 實現把服務接收到的信息返回給每一個客戶窗口中顯示功能之客戶接收

    nec 一個 out for tex ava 添加 implement com 客戶端要不斷接收服務端發來的信息 與服務端不斷接收客戶端發來信息相同,使用線程的方法,在線程中循環接收 客戶端修改後代碼如下: package com.swift; import java.

    使用axis呼叫webservice時,服務接收到的引數為null

    通過axis呼叫,需要注意兩點: 1)在call.setOperationName是必須通過Qname來制定namespaceURI 2)在設定引數時,不使用服務端定義的引數名,而是arg0~argN來定義,也不需制定namespaceURI,上述程式碼 call.addParamete

    將從服務接收到的資料轉成JSON資料

    第一步,從服務端接收資料,並將資料轉成int陣列。 try{ // 建立輸入流物件InputStream is = socket.getInputStream(); int length = is.a

    Dubbo原始碼解析之服務接收訊息

    準備 dubbo 版本:2.5.4 服務端接收訊息流程 Handler鏈路 DubboProtocol private ExchangeServer createServer(URL url) { url = url.addParameterIfAbsent("c

    PHP接收http請求頭信息

    定義函數 lac subst png 自定義 接收 style 自定義函數 col 1、PHP 自帶函數 getallheaders() 目前 getallheaders() 只能用於 apache 中。如果想在 nginx 中也能使用,可以使用自定義函數。 fore

    PHP接收http請求頭資訊

    1、PHP 自帶函式 getallheaders() 目前 getallheaders() 只能用於 apache 中。如果想在 nginx 中也能使用,可以使用自定義函式。 foreach (getallheaders() as $name => $value)

    c#Socket Tcp服務程式設計

    轉自  https://www.cnblogs.com/kellen451/p/7127670.html   1 /* 2 * 3 * 該類用於管理tcp連線通訊 4 * 5 */ 6 7 using System; 8

    java後傳送http請求使用RestTemplate(簡單的都無敵了)

    以前做專案,一聽到從後端傳送http請求,我就比較頭疼,因為要寫那麼一堆程式碼。 例如: String result= ""; BufferedReaderin = null; try { String urlNameS

    DataOutputStream使用writeBytes寫入中文資料時服務接收到的是亂碼

    在開發過程中發現使用HttpURLConnection傳送POST請求,需要同時上傳檔案和其他引數,引數中如果有中文字元,DataOutputStream使用writeBytes,將中文字元傳入的話,服務端接收到的字串會亂碼,但是使用write就不會,故截取了一部分程式碼進行

    springMvc接收Http請求獲取Json資料

    1、用httpServletRequest接收引數,用流的方式讀取body } 2、用spring封裝的@RequestBody JSONObject直接獲取http請求中的Body資料,要求      1).http請求中的ContentType必須為applic