• C# HttpRequest


      1 using System;
      2 using System.Collections;
      3 using System.Collections.Generic;
      4 using System.IO;
      5 using System.Linq;
      6 using System.Net;
      7 using System.Security.Policy;
      8 using System.Text;
      9 using BDIC_BASE.Bonc.UI.Util;
     10 
     11 namespace BDIC_BASE.Bonc.DAL
     12 {
     13     public class HttpRequest
     14     {
     15         public static string DoPost(string url, Hashtable paramsOfUrl)
     16         {
     17             if (url == null)
     18             {
     19                 throw new Exception("url 为空");
     20                 //return "";
     21             }
     22             // 编辑并Encoding提交的数据  
     23             byte[] data = GetJointBOfParams(paramsOfUrl);
     24 
     25             // 发送请求  
     26             System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
     27             request.Method = "POST";
     28             request.ContentType = "application/x-www-form-urlencoded";
     29             request.ContentLength = data.Length;
     30 
     31             Stream stream = request.GetRequestStream();
     32             stream.Write(data, 0, data.Length);
     33             stream.Close();
     34 
     35             // 获得回复  
     36             System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
     37             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
     38             string result = reader.ReadToEnd();
     39             reader.Close();
     40 
     41             return result;
     42         }
     43 
     44         public static string DoGet(string url, Hashtable paramsOfUrl)
     45         {
     46             if (url == null)
     47             {
     48                 throw new Exception("url 为空");
     49                 //return "";
     50             }
     51             // 编辑并Encoding提交的数据  
     52             string  data = GetJointSOfParams(paramsOfUrl);
     53             // 拼接URL
     54             url += "?" + data;
     55 
     56             // 发送请求  
     57             System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
     58             request.Method = "GET";
     59             
     60             // 获得回复  
     61             System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
     62             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
     63             string result = reader.ReadToEnd();
     64             reader.Close();
     65 
     66             return result;
     67         }
     68 
     69         private static String GetJointSOfParams(Hashtable paramsOfUrl)
     70         {
     71             // 编辑并Encoding提交的数据  
     72             StringBuilder sbuilder = new StringBuilder();
     73             int i = 0;
     74             foreach (DictionaryEntry de in paramsOfUrl)
     75             {
     76                 if (i == 0)
     77                 {
     78                     sbuilder.Append(de.Key + "=" + de.Value);
     79                 }
     80                 else
     81                 {
     82                     sbuilder.Append("&" + de.Key + "=" + de.Value);
     83                 }
     84             }
     85             return sbuilder.ToString();
     86         }
     87 
     88         private static byte[] GetJointBOfParams(Hashtable paramsOfUrl)
     89         {
     90             // 编辑并Encoding提交的数据  
     91             String stringJointOfParams =  GetJointSOfParams(paramsOfUrl);
     92             byte[] data = new ASCIIEncoding().GetBytes(stringJointOfParams);
     93 
     94             return data;
     95         }
     96     }
     97 
     98     public class HttpParam
     99     {
    100         public HttpParam()
    101         {
    102         }
    103 
    104         private Hashtable _paramsOfUrl;
    105         public Hashtable ParamsOfUrl
    106         {
    107             get
    108             {
    109                 if (_paramsOfUrl == null)
    110                 {
    111                     _paramsOfUrl = Hashtable.Synchronized(new Hashtable());
    112                 }
    113                 return _paramsOfUrl;
    114             }
    115             set { _paramsOfUrl = value; }
    116         }
    117 
    118         public void AddParamOfUrl(String paramKey ,String paramValue)
    119         {
    120             try
    121             {
    122                 ParamsOfUrl.Add(paramKey, paramValue);
    123             }
    124             catch (Exception ex)
    125             {
    126                 Console.WriteLine("可能为key重复
    详细:" + ex.Message);
    127             }
    128         }
    129     }
    130 }
  • 相关阅读:
    iOS开发-消息初认识
    小程序开发相关网址
    201703-4 地铁修建
    CCF 201703-3 Markdown
    UVALive 4998 Simple Encryption
    CCF 201609-4 交通规划
    CCF 201609-3 炉石传说
    UVALive 4270 Discrete Square Roots
    CCF 201604-2 俄罗斯方块
    codeforces 710E Generate a String
  • 原文地址:https://www.cnblogs.com/-wangjiannan/p/3519131.html
Copyright © 2020-2023  润新知