zoukankan      html  css  js  c++  java
  • 一个HttpWebRequest工具类

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    
    
    public class HttpRequestHelper
    {
        /// <summary>
        /// 提交数据到某网页
        /// </summary>
        public static string PostToUrl(string requestUrl, byte[] byteArrayPost, Encoding encoding)
        {
            string stringResponse = "";
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArrayPost.Length;
            webRequest.CookieContainer = new CookieContainer();
            webRequest.Credentials = CredentialCache.DefaultCredentials;
    
            Stream newStream = webRequest.GetRequestStream();
            //写入参数
            newStream.Write(byteArrayPost, 0, byteArrayPost.Length);
            newStream.Close();
    
            WebResponse webResponse = webRequest.GetResponse();
    
            StreamReader responseStream = new StreamReader(webResponse.GetResponseStream(), encoding);
            stringResponse = responseStream.ReadToEnd();
            webResponse.Close();
            responseStream.Close();
            return stringResponse;
        }
    
        /// <summary>
        /// 提交数据到某网页
        /// </summary>
        public static string GetToUrl(string requestUrl, Encoding encoding)
        {
    
            string stringResponse = "";
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
            webRequest.Method = "Get";
            webRequest.ContentType = "application/x-www-form-urlencoded";
    
            webRequest.CookieContainer = new CookieContainer();
            webRequest.Credentials = CredentialCache.DefaultCredentials;
            WebResponse webResponse = webRequest.GetResponse();
    
            StreamReader responseStream = new StreamReader(webResponse.GetResponseStream(), encoding);
            stringResponse = responseStream.ReadToEnd();
            webResponse.Close();
            responseStream.Close();
            return stringResponse;
        }
    }
    
    
  • 相关阅读:
    shell script数组使用函数输出
    Yii2文件上传
    ubuntu 安装遇到黑屏
    使用函数
    ionCube 安装
    记录LNMP环境彻底删除绑定域名及网站文件夹/文件的过程
    lnmp环境 开启pathinfo
    国外知名设计教程网址收集
    26个国外在线教育网站
    前端学习网站汇总
  • 原文地址:https://www.cnblogs.com/leiwei/p/3439586.html
Copyright © 2011-2022 走看看