【C#基礎】HTTP發送POST二進制數據


//postdata為數組的請求方式
public byte[] POST(string Url, byte[] byteRequest)
        {
            byte[] responsebody;
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {
                //如果是發送HTTPS請求
                if (Url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);
                    httpWebRequest.ProtocolVersion = HttpVersion.Version10;
                }
                else
                {
                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);//創建連接請求
                }
                httpWebRequest.Method = "POST";
                if (cookieContainer != null)
                {
                    httpWebRequest.CookieContainer = cookieContainer;
                }
                httpWebRequest.AllowAutoRedirect = AllowAutoRedirect;//【注意】這里有個時候在特殊情況下要設置為否,否則會造成cookie丟失
                httpWebRequest.ContentType = ContentType;
                httpWebRequest.Accept = Accept;
                httpWebRequest.UserAgent = UserAgent;
                if (!string.IsNullOrEmpty(uuid))
                {
                    httpWebRequest.Headers.Add("seed:" + uuid + "");
                }

                //Post請求數據,則寫入傳的PostData
                //byte[] byteRequest = Encoding.Default.GetBytes(PostData);
                httpWebRequest.ContentLength = byteRequest.Length;
                using (Stream stream = httpWebRequest.GetRequestStream())
                {
                    stream.Write(byteRequest, 0, byteRequest.Length);
                }
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//開始獲取響應流
                Stream responseStream = httpWebResponse.GetResponseStream();
                responsebody = StreamToBytes(responseStream);
                responseStream.Close();
                httpWebRequest.Abort();
                cookieContainer.Add(httpWebResponse.Cookies);
                cookieCollection.Add(httpWebResponse.Cookies);
                httpWebResponse.Close();
                //到這里為止,所有的對象都要釋放掉,以免內存像滾雪球一樣
            }
            catch (Exception ex)
            {
                responsebody = Encoding.Default.GetBytes(ex.Message + ex.Source);
                LogHelper.Log.Error("POST方式請求網頁異常", ex);
            }
            return responsebody;
        }

 

//postdata為數組的請求方式

public byte[] POST(string Url, byte[] byteRequest)

        {

            byte[] responsebody;

            HttpWebRequest httpWebRequest = null;

            HttpWebResponse httpWebResponse = null;

            try

            {

                //如果是發送HTTPS請求

                if (Url.StartsWith("https", StringComparison.OrdinalIgnoreCase))

                {

                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);

                    httpWebRequest.ProtocolVersion = HttpVersion.Version10;

                }

                else

                {

                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);//創建連接請求

                }

                httpWebRequest.Method = "POST";

                if (cookieContainer != null)

                {

                    httpWebRequest.CookieContainer = cookieContainer;

                }

                httpWebRequest.AllowAutoRedirect = AllowAutoRedirect;//【注意】這里有個時候在特殊情況下要設置為否,否則會造成cookie丟失

                httpWebRequest.ContentType = ContentType;

                httpWebRequest.Accept = Accept;

                httpWebRequest.UserAgent = UserAgent;

                if (!string.IsNullOrEmpty(uuid))

                {

                    httpWebRequest.Headers.Add("seed:" + uuid + "");

                }

 

                //Post請求數據,則寫入傳的PostData

                //byte[] byteRequest = Encoding.Default.GetBytes(PostData);

                httpWebRequest.ContentLength = byteRequest.Length;

                using (Stream stream = httpWebRequest.GetRequestStream())

                {

                    stream.Write(byteRequest, 0, byteRequest.Length);

                }

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//開始獲取響應流

                Stream responseStream = httpWebResponse.GetResponseStream();

                responsebody = StreamToBytes(responseStream);

                responseStream.Close();

                httpWebRequest.Abort();

                cookieContainer.Add(httpWebResponse.Cookies);

                cookieCollection.Add(httpWebResponse.Cookies);

                httpWebResponse.Close();

                //到這里為止,所有的對象都要釋放掉,以免內存像滾雪球一樣

            }

            catch (Exception ex)

            {

                responsebody = Encoding.Default.GetBytes(ex.Message + ex.Source);

                LogHelper.Log.Error("POST方式請求網頁異常", ex);

            }

            return responsebody;

        }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM