• 一个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;
        }
    }
    
    
  • 相关阅读:
    C# 利用 Geckofx60 实现下载
    C# 线程 线程池
    C# DateTime 与 String 格式转换
    C# WPF 获取程序路径
    C# 计时器 Timer 介绍
    获取远程图片并把它保存到本地
    php sql 过滤
    PHP如何生成伪静态
    用php获取客户端IP地址的方法
    php过滤危险html代码
  • 原文地址:https://www.cnblogs.com/leiwei/p/3439586.html
Copyright © 2020-2023  润新知