httphelper

public class HttpHelper
    {
        public static string Post(string url, string paramData)
        {
            return Post(url, paramData, Encoding.UTF8);
        }

        public static string Post(string url, string paramData, Encoding encoding)
        {
            string result;

            if (url.ToLower().IndexOf("https", System.StringComparison.Ordinal) > -1)
            {
                ServicePointManager.ServerCertificateValidationCallback =  new RemoteCertificateValidationCallback((sender, certificate, chain, errors) => { return true; });
            }

            try
            {
                var wc = new WebClient();
                if (string.IsNullOrEmpty(wc.Headers["Content-Type"]))
                {
                    wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                }
                wc.Encoding = encoding;

                result = wc.UploadString(url, "POST", paramData);
            }
            catch (Exception e)
            {
                throw;
            }

            return result;
        }

        public static string Get(string url)
        {
            return Get(url, Encoding.UTF8);
        }

        public static string Get(string url, Encoding encoding)
        {
            try
            {
                var wc = new WebClient { Encoding = encoding };
                var readStream = wc.OpenRead(url);
                using (var sr = new StreamReader(readStream, encoding))
                {
                    var result = sr.ReadToEnd();
                    return result;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static string PostData(string url, NameValueCollection parameters)
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            Encoding encoding = Encoding.UTF8;
            var parassb = new StringBuilder();
            foreach (string key in parameters.Keys)
            {
                if (parassb.Length > 0)
                {
                    parassb.Append("&");
                }
                parassb.AppendFormat("{0}={1}", key, HttpUtility.UrlEncode(parameters[key]));
            }

            byte[] bs = Encoding.UTF8.GetBytes(parassb.ToString());

            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
                reqStream.Close();
            }

            string result = String.Empty;
            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                {
                    result = reader.ReadToEnd();
                }
                return result;
            }
        }
    }  

使用示例:

      var data = "{\"touser\":\"Rich\" },";
          data += "{\"msgtype\": \"text\"},";
         data += "{\"text\": [{\"content\": \"test\"}]}";
        var json = HttpHelper.Post("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken, data);  

var parameters = new NameValueCollection { { "Aid", dModel.Aid }, { "Oid", dModel.Oid }, { "Uid", dModel.Uid }, { "Cid", dModel.Coupon }, { "CidD", dModel.Usedt.ToString() }, { "Info", dModel.Info }, { "Return", dModel.ReturnVal } };
HttpHelper.PostData("http://www.baidu.com", parameters);
时间: 2024-07-30 14:11:35

httphelper的相关文章

抓取百万知乎用户信息之HttpHelper的迭代之路

什么是Httphelper? httpelpers是一个封装好拿来获取网络上资源的工具类.因为是用http协议,故取名httphelper. httphelper出现的背景 使用WebClient可以很方便获取网络上的资源,例如 WebClient client = new WebClient(); string html= client.DownloadString("https://www.baidu.com/"); 这样就可以拿到百度首页的的源代码,由于WebClient封装性太

HttpWebRequest 模拟登录响应点击事件(开源自己用的HttpHelper类)

平时也经常采集网站数据,也做模拟登录,但一般都是html控件POST到页面登录:还没有遇到用户服务器控件button按钮点击事件登录的,今天像往常一样POST传递参数,但怎么都能登录不了:最后发现还有两个参数需要传,__EVENTVALIDATION和__VIEWSTATE 在传的过程中需要对参数值进行URL编码 System.Web.HttpUtility.UrlEncode(value) 模拟登录代码:在本地写的一个测试的网站来模拟登录,原理都一样: Request request = ne

HttpHelper万能框架GetMergeCookie的问题

用万能框架写了一个DZ带验证码POST登录一直错误 http://www.sufeinet.com/thread-17795-1-1.html 调试半天发现是框架GetMergeCookie的问题,,,真坑... OldCookie F9ZL_2132_saltkey=oHLnJLwj;F9ZL_2132_lastvisit=1477040054; F9ZL_2132_sid=ZsTR8p; F9ZL_2132_lastact=1477043654%09member.php%09logging;

HttpHelper工具类

/// <summary> /// 类说明:HttpHelper类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理 /// 重要提示:请不要自行修改本类,如果因为你自己修改后将无法升级到新版本.如果确实有什么问题请到官方网站提建议, /// 我们一定会及时修改 /// 编码日期:2011-09-20 /// 编 码 人:苏飞 /// 联系方式:361983679 /// 官方网址:http://www.sufeinet.com/thre

HttpHelper类

/// <summary> /// 类说明:HttpHelper类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理 /// 重要提示:请不要自行修改本类,如果因为你自己修改后将无法升级到新版本.如果确实有什么问题请到官方网站提建议, /// 我们一定会及时修改 /// 编码日期:2011-09-20 /// 编 码 人:苏飞 /// 联系方式:361983679 /// 官方网址:http://www.sufeinet.com/thre

HttpHelper之我见

前几月一直用一个Http的访问类去调用WebApi,说句实话最开始没觉有什么,一是技术老,二是觉得比较简单,但是最近我一直关注云开发和AI这块儿微软技术,看到云平台调用API大多类似,所以回想这个早年的调用方式,感觉有老汤炖肉之感,实用又有价值,可以作为认识调用API的学习和使用,回味无穷. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; u

使用苏飞httphelper开发自动更新发布文章程序

最近新上线了一个网站,专门收集网上签到赚钱,有奖活动等等的网站 我就要集分宝 http://www.591jfb.com.新建立 了一个栏目"每日更新",这样就需要每天都登录到网站后台去发布文章,感觉有些繁琐,于是就想找点省劲的办法,于是便有了此文. 搜索下载了苏飞提供的httphelper,比着例子写了一下程序,结果返回的html总是错误页,于是又翻sufeinet论坛上面的帖子,搜索到有人用苏飞开发助手测试远程发布,于是也下载下来测试了一下,结果成功了,对照苏飞开发助手生成的代码和

C#使用HttpHelper万能框架,重启路由器

首先声明,不是所有路由器都可以通过下面的代码来让路由器执行重启. 下面的代码测试的路由器是(TP-LINK TD-W89841N增强型).要根据自己的路由器来写代码. 1 using CsharpHttpHelper; //引用HttpHelper类库 2 using System; 3 using System.Text; 4 5 namespace ConsoleApplication1 6 { 7 class Program 8 { 9 static void Main(string[]

完整HttpHelper类

[代码] [C#]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82