1. 程式人生 > >c# 傳送http請求

c# 傳送http請求

/// 傳送請求  
        /// </summary>  
        /// <param name="url">請求地址</param>  
        /// <param name="sendData">引數格式 “name=王武&pass=123456”</param>  
        /// <returns></returns>  
        public static string RequestWebAPI(string url, string sendData)  
        {  
            string backMsg = "";  
            try  
            {  
                System.Net.WebRequest httpRquest = System.Net.HttpWebRequest.Create(url);  
                httpRquest.Method = "POST";  
                //這行程式碼很關鍵,不設定ContentType將導致後臺引數獲取不到值  
                httpRquest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";  
                byte[] dataArray = System.Text.Encoding.UTF8.GetBytes(sendData);  
                //httpRquest.ContentLength = dataArray.Length;  
                System.IO.Stream requestStream = null;  
                if (string.IsNullOrWhiteSpace(sendData) == false)  
                {  
                    requestStream = httpRquest.GetRequestStream();  
                    requestStream.Write(dataArray, 0, dataArray.Length);  
                    requestStream.Close();  
                }  
                System.Net.WebResponse response = httpRquest.GetResponse();  
                System.IO.Stream responseStream = response.GetResponseStream();  
                System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8);  
                backMsg = reader.ReadToEnd();  
  
  
                reader.Close();  
                reader.Dispose();  
  
  
                requestStream.Dispose();  
                responseStream.Close();  
                responseStream.Dispose();  
            }  
            catch (Exception)  
            {  
                throw;  
            }  
            return backMsg;  
        }

相關推薦

C傳送http請求

之前用python編寫了傳送http請求的。非常非常方便。很多細節都已經被python內部處理了。這裡來說說C編寫的http請求: #include <stdio.h> #include <string.h> #include <sys/soc

C++傳送HTTP請求---親測可行(轉)

轉自:http://hi.baidu.com/benbearlove/item/1671c23017575825b3c0c53f環境:xp sp3,vs2008,在靜態庫中使用 MFC#include <afxwin.h>#include <stdio.h&

c# 傳送http請求

/// 傳送請求 /// </summary> /// <param name="url">請求地址</param> /// <param name="sendData"&g

C# 傳送Http協議 模擬 Post Get請求

1.引數 paramsValue的格式 要和 Reques.ContentType一致, 如果 contentype  "application/x-www-form-urlencoded" 表單型別,那麼  引數為   a=1&b=2 形式 如果 。。

Linux下用c語言實現傳送http請求 方式可以Get或者Post例程參考

[1].[程式碼] Linux下用c語言實現傳送http請求 方式可以Get或者Post 跳至 [1] ? 1 2

Linux下用c語言實現傳送http請求

前言 在linux下,使用socket進行程式設計,需要到伺服器上進行獲取資料,伺服器使用的php程式設計,需要使用http的方式進行獲取資料。 程式碼 #include <stdio.h> #include <string.h&

C socket 傳送HTTP請求

HTTP請求頭部樣例: GET http://www.baidu.com/ HTTP/1.1 Accept: html/text Host: 220.181.6.175:80 Connection: Close 這是一個請求百度頁面的頭部。 屬性和值的命名中間用:和空格隔

Windows C下利用wininet傳送http請求

在Windows下,通過使用wininet庫傳送http請求,以下是程式碼例項: char szBuffer[1024] = {0};//宣告全域性變數,作為服務端返回的資料 void SendHttpRequest(char ip[], int port, char

C/C++使用libcurl庫傳送http請求

C++要實現http網路連線,需要藉助第三方庫,libcurl使用起來還是很方便的 環境:win32 + vs2015 如果要在linux下使用,用cmake編譯,使用基本同理 1,下載編譯libcurl 下載curl原始碼,找到vs工程,按照x86 x64 並對

C++ 模擬瀏覽器傳送HTTP請求

#include <afxwin.h> #include <stdio.h> #include <windows.h> #include <string> #include "Wininet.h" #pragma commen

C#後臺傳送HTTP請求

HttpResponse 1.涵蓋POST,GET方法 using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; usi

C#通過GET/POST方式傳送Http請求

介紹http請求的兩種方式,get和post方式。並用C#語言實現,如何請求url並獲取返回的資料 兩者的區別: 引數 Get請求把提交的資料進行簡單編碼,同時將url的一部分發送到伺服器 比如url:Http://127.0.0.1/login.j

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

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

網絡通信第一課 C++封裝HTTP請求報文說明

c++ http post一個HTTP請求報文由請求行(request line)、請求頭部(header)、空行和請求數據4個部分組成使用C++組裝上述報文 boost::asio::streambuf request; std::ostream request_stream(&request);

C#模擬HTTP請求並發送二進制

keep lose type response ica con new urn for public static String Submit(String methodName) { string postData = "this is post data";/

java中使用Apache HttpClient傳送Http請求,並獲取返回結果

傳送http請求可以寫成一個工具類,HttpClient可以使用連線池建立,這樣的好處是我們可以自己定義一些配置,比如請求超時時間,最大連線數等等。 public class HttpUtil { private static CloseableHttpClient http

使用feign傳送http請求解析報錯

錯誤如下: 傳送請求開始 ----- [ChannelFeign#formRecog] ---> END HTTP (304117-byte body) 傳送請求結束 返回開始 [ChannelFeign#formRecog] <--- HTTP/1.1 200 OK (4948ms

Python傳送Http請求時,提交中文或者符號中文編碼問題的解決方法

前言 博主最近在用python3比較強大的Django開發web的時候,發現一些url的編碼問題,在瀏覽器提交請求api時,如果url中包含漢子,就會被自動編碼掉。呈現的結果是 ==> %xx%xx%xx。如果出現3個百分號為一個原字元則為utf8編碼,如果2個百分號則為gb2312編碼。

nodejs使用request傳送http請求

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/dreamer2020/article/details/52074516 在nodejs的開發中,有時需要後臺去呼叫其他伺服器的介面,這個時候,就需要傳送HTTP請求了。有一個簡單的工具可以用,Si

JS傳送http請求(get方式)

var url = "http://192.168.2.234:8080/GS1014J_MCF/forumServer?" + "queryMode=byId&forumId=297e254c60114cd301601186c0740009"; var time = 1000;