zoukankan      html  css  js  c++  java
  • C# Http POST get

    using System.IO;
    using System.Net;

    /// <summary>
            /// HttpWebRequest发送Post请求
         /// </summary>
         /// <param name="postUrl"></param>
         /// <param name="paramData"></param>
         /// <param name="dataEncode"></param>
         /// <returns></returns>
            public static string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
            {
                string ret = string.Empty;
                byte[] byteArray = dataEncode.GetBytes(paramData);
                //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                //写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), dataEncode);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
                return ret; }
           /// <summary>
            /// WebClient发送Get请求,编码UTF8
           /// </summary>
           /// <param name="Url"></param>
           /// <returns></returns>
            public static string Get(string Url) {
                string result = "";
          
                HttpWebRequest httpReq;
                httpReq = (HttpWebRequest)WebRequest.Create(new Uri(Url));
                WebResponse wr = httpReq.GetResponse();
                Stream responseStream = wr.GetResponseStream();
                StreamReader respStreamReader = new StreamReader(responseStream);
                result = respStreamReader.ReadToEnd();
                responseStream.Dispose(); wr.Dispose();
                return result;
            }

  • 相关阅读:
    ASP.NET Core开发者路线指南(转)
    一文读懂QPS、TPS、PV、UV、GMV、IP、RPS(转)
    后端开发术语大全转
    css 动态设置某一元素随浏览器大小而调整
    .NET FTP上传文件
    bootstrapselectpicker 插件事件
    Node.js安装及环境配置之Windows篇
    EasyUI表单验证插件扩展
    程序员需要知道的缩写和专业名词转
    JavaScript指定日期格式化
  • 原文地址:https://www.cnblogs.com/Andy-Blog/p/5393642.html
Copyright © 2011-2022 走看看