日韩欧美人妻无码精品白浆,www.大香蕉久久网,狠狠的日狠狠的操,日本好好热在线观看

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#封裝HttpWebRequest GET POST PUT DELETE

freeflydom
2025年7月1日 16:37 本文熱度 97
?/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描    述:Http請(qǐng)求工具類(lèi)
*│  Get     :像數(shù)據(jù)庫(kù)的select,只是用來(lái)查詢(xún)一下數(shù)據(jù),不會(huì)修改、增加數(shù)據(jù),不會(huì)影響資源的內(nèi)容。
*│  Post    :像數(shù)據(jù)庫(kù)的insert操作一樣,會(huì)創(chuàng)建新的內(nèi)容。幾乎目前所有的提交操作都是用POST請(qǐng)求的。
*│  Put     :像數(shù)據(jù)庫(kù)的update操作一樣,用來(lái)修改數(shù)據(jù)的內(nèi)容,但是不會(huì)增加數(shù)據(jù)的種類(lèi)等。
*│  Delete  :像數(shù)據(jù)庫(kù)的delete操作
*│ 作    者:執(zhí)筆小白
*│ 版    本:2.1                                   
*│ 創(chuàng)建時(shí)間:2021-10-20 15:40:56~2023-03-25 22:42:56                            
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空間: WebserviceWcfWebAPITestTool.ASPNetCoreWebAPI_Test                             
*│ 類(lèi)    名:WebAPITestForm                                     
*└──────────────────────────────────────────────────────────────┘
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using static System.Net.WebRequestMethods;
namespace CommonTools
{
    // 請(qǐng)求工具類(lèi)
    // HttpWebRequest(WebRequest.Create):.NET.Framework的請(qǐng)求/響應(yīng)模型的抽象基類(lèi),用于訪(fǎng)問(wèn)Internet數(shù)據(jù)。
    // HttpWebResponse:對(duì)http協(xié)議進(jìn)行了完整的封裝( Header, Content, Cookie),與HttpWebRequest結(jié)合使用。
    public class RequestCom
    {
        #region WebAPI
        /// <summary>
        /// Get方法
        /// </summary>
        /// 例如:http://localhost:30202/api/ValuesTest/Sum?num1=1&num2=3
        /// <param name="postData">后綴(?num1=1&num2=3)</param>
        /// <param name="Url">url(http://localhost:30202/api/ValuesTest/Sum)</param>
        /// <returns></returns>
        public static string GetInfo(string postData, string Url)
        {
            try
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
                webRequest.Method = "GET";
                webRequest.ContentType = "application/json; charset=utf-8";
                webRequest.ContentLength = byteArray.Length;
                webRequest.Accept = "application/json, text/javascript, */*";
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {
                    return sr.ReadToEnd(); // 返回的數(shù)據(jù)
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// Post請(qǐng)求
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="body">application/json</param>
        /// <returns></returns>
        public static string HttpPost(string url, string body)
        {
            try
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(body);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Method = "POST";
                webRequest.ContentType = "application/json; charset=utf-8";
                webRequest.ContentLength = byteArray.Length;
                webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);
                webRequest.Accept = "application/json, text/javascript, */*";
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// Put請(qǐng)求-必有body
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="body">application/json</param>
        /// <returns></returns>
        public static string HttpPut(string url, string body)
        {
            try
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(body);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Method = "PUT";
                webRequest.ContentType = "application/json";
                webRequest.ContentLength = byteArray.Length;
                webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);
                webRequest.Accept = "application/json, text/javascript, */*";
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// Delete請(qǐng)求-必有body
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="body">application/json</param>
        /// <returns></returns>
        public static string HttpDelete(string url, string body)
        {
            try
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(body);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Method = "DELETE";
                webRequest.ContentType = "application/json";
                webRequest.ContentLength = byteArray.Length;
                webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);
                webRequest.Accept = "application/json, text/javascript, */*";
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
        #region WebService
        /// <summary>
        /// Set
        /// </summary>
        /// <summary>
        /// Post方法-拼接Body組方式:ReqBody參數(shù)組(Key,Value)
        /// </summary>
        /// <param name="url">webService的URL</param>
        /// <param name="method">調(diào)用的方法</param>
        /// <param name="reqBodys">參數(shù)組合</param>
        /// <returns></returns>
        public static string WebServiceHttpPost(string URL, string Method, List<ReqBody> ReqBodys, Encoding requestCoding, int timeout = 30000)
        {
            string param = string.Empty;
            switch (ReqBodys.Count)
            {
                case 0:
                    break;
                case 1:
                    param = HttpUtility.UrlEncode(ReqBodys[0].Key) + "=" + HttpUtility.UrlEncode(ReqBodys[0].Value);
                    break;
                default:
                    param = HttpUtility.UrlEncode(ReqBodys[0].Key) + "=" + HttpUtility.UrlEncode(ReqBodys[0].Value);
                    for (int i = 1; i < ReqBodys.Count; i++)
                    {
                        param += "&" + HttpUtility.UrlEncode(ReqBodys[i].Key) + "=" + HttpUtility.UrlEncode(ReqBodys[i].Value);
                    }
                    break;
            }
            //byte[] byteArray = Encoding.UTF8.GetBytes(param);
            byte[] byteArray = requestCoding.GetBytes(param);
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL + "/" + Method);
            webRequest.Method = "POST";
            webRequest.Timeout = timeout;
            // webRequest.UserAgent = "DefaultUserAgent";
            webRequest.ContentType = "application/x-www-form-urlencoded";  // 瀏覽器默認(rèn)的編碼格式
            webRequest.ContentLength = byteArray.Length;
            webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);       //把參數(shù)數(shù)據(jù)寫(xiě)入請(qǐng)求數(shù)據(jù)的Stream對(duì)象
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();      //獲得響應(yīng)
            #region 只返回Response的Xml報(bào)文(Body內(nèi)容)
            using (XmlTextReader reader = new XmlTextReader(webResponse.GetResponseStream()))  //獲取響應(yīng)流
            {
                reader.MoveToContent();
                return reader.ReadInnerXml();
            }
            #endregion 只返回Response的Xml報(bào)文(Body內(nèi)容)
            #region 返回所有Xml報(bào)文
            //using(StreamReader sr = new StreamReader(webResponse.GetResponseStream(), requestCoding))
            //{
            //    return sr.ReadToEnd();
            //}
            #endregion 返回所有Xml報(bào)文
        }
        /// <summary>
        /// Post方法-拼接xml方式
        /// 下面有示例"Post方法-拼接xml方式示例"
        /// </summary>
        /// <param name="url">webService的URL</param>
        /// <param name="soapAction">soap方法,可為null</param>
        /// <param name="soap_Namespace">soap的命名空間</param>
        /// <param name="soap_EnvelopeXml">soap:Envelope的信息</param>
        /// <param name="soap_HeaderXml">soap:Header的信息</param>
        /// <param name="soap_BodyXml">soap:Body的信息</param>
        /// <param name="requestCoding">編碼格式</param>
        /// <param name="timeout">超時(shí)</param>
        /// <returns></returns>
        public static string WebServiceHttpPost(string url, string soapAction, string soap_Namespace, string soap_EnvelopeXml, string soap_HeaderXml, string soap_BodyXml, Encoding requestCoding, int timeout = 30000)
        {
            // 確認(rèn)編碼
            string requestCodingStr = "UTF-8";
            switch (requestCoding)
            {
                case UTF8Encoding:
                    requestCodingStr = "UTF-8";
                    break;
                case UTF32Encoding:
                    requestCodingStr = "UTF-32";
                    break;
                case ASCIIEncoding:
                    requestCodingStr = "ASCII";
                    break;
                default:
                    break;
            }
            string requestXml = GetPostStr(requestCodingStr, soap_Namespace, soap_EnvelopeXml, soap_HeaderXml, soap_BodyXml);  // 拼接xml
            //byte[] byteArray = Encoding.UTF8.GetBytes(requestXml);
            byte[] byteArray = requestCoding.GetBytes(requestXml);
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = timeout;
            //httpWebRequest.ContentType = "application/x-www-form-urlencoded";  // 瀏覽器默認(rèn)的編碼格式
            httpWebRequest.ContentType = $"text/xml;charset={requestCodingStr}";  // xml編碼格式
            if (soapAction != null)
            {
                httpWebRequest.Headers.Add("SOAPAction", soapAction);  // SOAP方法,有的需要設(shè)置(SOAP 1.1不一定需要;SOAP1.2不需要設(shè)置)
            }
            //httpWebRequest.Headers.Add("Accept-Language", "zh-cn,en-US,en;q=0.5");
            //httpWebRequest.Headers.Add("Cache-Control", "no-cache");
            //httpWebRequest.UserAgent = "DefaultUserAgent";
            httpWebRequest.ContentLength = byteArray.Length;
            httpWebRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);       // 把參數(shù)數(shù)據(jù)寫(xiě)入請(qǐng)求數(shù)據(jù)的Stream對(duì)象
            // 接收返回信息
            HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            #region 只返回Response的Xml報(bào)文(Body內(nèi)容)
            using (XmlTextReader reader = new XmlTextReader(webResponse.GetResponseStream()))  //獲取響應(yīng)流
            {
                reader.MoveToContent();
                return reader.ReadInnerXml();
            }
            #endregion 只返回Response的Xml報(bào)文(Body內(nèi)容)
            #region 返回所有Xml報(bào)文
            //using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), requestCoding))  // 返回Xml格式的字符串
            //{
            //    return sr.ReadToEnd();
            //}
            #endregion 返回所有Xml報(bào)文
        }
        // Post方法-拼接xml方式示例
        //private void button1_Click(object sender, EventArgs e)
        //{
        //    string soap_Namespace = "soap";
        //string soap_EnvelopeXml = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"";
        //string soap_HeaderXml = string.Empty;
        //string soap_BodyXml = " <HelloWorld xmlns=\"http://tempuri.org/\" />";
        //Encoding requestCoding = Encoding.UTF8;
        //string result = RequestCom.WebServiceHttpPost(url, soap_Namespace, soap_EnvelopeXml, soap_HeaderXml, soap_BodyXml, requestCoding);
        //textBox5.Text = result;
        //}
        /// <summary>
        /// 拼接HttpWebResponse的RequestStream
        /// </summary>
        /// <param name="requestCodingStr">編碼格式</param>
        /// <param name="soap_Namespace">soap的命名空間</param>
        /// <param name="soap_EnvelopeXml">soap:Envelope的信息</param>
        /// <param name="soap_HeaderXml">soap:Header的信息</param>
        /// <param name="soap_BodyXml">soap:Body的信息</param>
        private static string GetPostStr(string requestCodingStr, string soap_Namespace, string soap_EnvelopeXml, string soap_HeaderXml, string soap_BodyXml)
        {
            // 拼接參數(shù)
            string postStr = string.Empty;
            postStr = $"<?xml version=\"1.0\" encoding=\"{requestCodingStr}\"?> ";
            // soap:Envelope的信息
            //<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
            //<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
            //<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tip=""http://www.digiwin.com.cn/tiptop/TIPTOPServiceGateWay"">
            postStr += $"<{soap_Namespace}:Envelope " + soap_EnvelopeXml + ">";
            // soap:Header的信息
            //<soap:Header></soap:Header>
            //<soap12:Header></soap12:Header>
            //<soapenv:Header></soapenv:Header>
            postStr += $"<{soap_Namespace}:Header>" + soap_HeaderXml + $"</{soap_Namespace}:Header>";
            // soap:Body的信息
            //<soap12:Body>
            //<HelloWorld xmlns="http://tempuri.org/" />
            //</soap12:Body>
            postStr += $"<{soap_Namespace}:Body>" + soap_BodyXml + $"</{soap_Namespace}:Body>";
            postStr += $"</{soap_Namespace}:Envelope>";
            return postStr;
        }
        #endregion WebService
    }
    // 參數(shù)
    public class ReqBody
    {
        /// <summary>
        /// 參數(shù)名
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 參數(shù)值
        /// </summary>
        public string Value { get; set; }
    }
}

轉(zhuǎn)自https://www.cnblogs.com/qq2806933146xiaobai/p/15397848.html


該文章在 2025/7/1 16:37:55 編輯過(guò)
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專(zhuān)業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車(chē)隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類(lèi)企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷(xiāo)售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶(hù)的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

高级无码按摩国产一区二区| 中文字幕欧美日韩国产经典综合| 超碰亚洲91| 极品中文字幕不卡综合一区二区| 五月亭亭激情综合| 自拍爱一线| 国产一区无码高清观看| 专干老熟女6070| 日日日色婷婷一区二区| 欧美日三级视频| 在线亚洲一区二区| 日本在线看片一区| 乃东县| 兴国县| 超碰在线主播| 日本一区不卡高清| 美女操逼逼晓晓视频| 春宵夜福利导航| 熟美国毛片免| 91久久女同视频| 中出后入少妇人妻| 五十路熟女在线91| 黑人xxxxx中文大鸡巴| 天堂东京热AV| 曰韩无码中文字幕免费观看 | 亚洲初爱av中文在线观看| 香蕉蜜臀AV在线| 这里有精品7| 亚洲精品欧美综合国产图片区| 区美一级一区二区| 三级人人操| 啊。。。好舒服在线观看| 色夜精品一区二区三区| 人人噜人人操| 日韩一区二区成人影院| 亚洲欧洲无破一区二区卡99| 日本理论网站在线播放| 久久久中文字幕捆绑| 日韩系列一97人妻| 无码电影AV在线| 欧美少妇综合一区|