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;
        }

随机推荐

  1. MongoDB 复制集 (一) 成员介绍

       一 MongoDB 复制集简介          MongoDB的复制机制主要分为两种:          Master-Slave    (主从复制)      这个已经不建议使用       ...

  2. Char* ,CString ,WCHAR*之间的转换

    关于Char* ,CString ,WCHAR*之间的转换问题 GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用 ...

  3. Least Common Multiple

    地址:http://www.codewars.com/kata/5259acb16021e9d8a60010af/train/python 题目: Write a function that calc ...

  4. Linux进程间通信——使用数据报套接字

    前一篇文章, Linux进程间通信——使用流套接字介绍了一些有关socket(套接字)的一些基本内容,并讲解了流套接字的使用,这篇文章将会给大家讲讲,数据报套接字的使用. 一.简单回顾——什么是数据报 ...

  5. How to trace a java-program

    up vote17down votefavorite 8 As a sysadmin I sometimes face situations, where a program behaves abno ...

  6. linux shell less 命令---转

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  7. Android界面的View以及ViewGroup的区别

    因为这个问题会经常成为面试的热点,所以我们来谈谈View以及ViewGroup的区别. 先看看View及ViewGroup类关系    Android View和ViewGroup从组成架构上看,似乎 ...

  8. Google Map API v2 步步为营 (二)----- Location

    接上篇. 改造一下MapsActivity: public class MapsActivity extends Activity implements LocationListener, InfoW ...

  9. SDWebImage 源码阅读分享

    SDWebImage 源码阅读分享 疑问列表 SDWebImage 整体框架图,主要的类包含哪些 SDWebImage 如何进行缓存管理,过期失效策略,缓存更新 SDWebImage 如何多线程处理的 ...

  10. step2 uboot tag存储主要部分代码

      cmd_bootm.c //传递给内核的参数      int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])     ...