• 大小: 26KB
    文件类型: .cs
    金币: 2
    下载: 1 次
    发布日期: 2022-09-07
  • 语言: C#
  • 标签: C#  HttpWebReaue  Get  Post  

资源简介

包括了HttpGet同步请求、异步请求方法,HttpPost同步请求、异步请求方法,以及Post调用WebServer方法。

资源截图

代码片段和文件信息

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.xml;
using System.xml.Serialization;

namespace ivw.Common
{
    /// 
    /// HttpHelper
    /// 

    public class HttpHelper
    {
        #region string Get(String sUri)
        /// 
        /// Get
        /// 

        /// 
        /// 
        public static string Get(String sUri)
        {
            string sRet = string.Empty;
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(sUri);
                httpWebRequest.Method = “GET“;
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (Stream stream = httpWebResponse.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        sRet = streamReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                sRet = ex.Message;
            }
            return sRet;
        }
        #endregion

        #region string Get(String sUri Dictionaryject> dicParams)
        /// 
        /// Get
        /// 

        /// 
        /// 
        /// 
        public static string Get(String sUri Dictionaryject> dicParams)
        {
            string sRet = string.Empty;
            try
            {
                StringBuilder sb = new StringBuilder();
                int i = 0;
                foreach (var item in dicParams)
                {
                    if (i > 0)
                    {
                        sb.Append(“&“);
                    }
                    sb.AppendFormat(“{0}={1}“ item.Key item.Value);
                    i++;
                }
                if (sUri.Substring(sUri.Length - 1 1) == “?“)//判断最后一个字符是否是?号
                {
                    sUri += $“{sb.ToString()}“;
                }
                else
                {
                    sUri += $“?{sb.ToString()}“;
                }

                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(sUri);
                httpWebRequest.Method = “GET“;
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (Stream stream = httpWebResponse.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        sRet = streamReader.ReadToEnd();
   

评论

共有 条评论