(自备)HttpWebRequest

public static string CreateRequest(string reqUrl, string method, bool useProxy, Encoding code, string param)
        {
            string resultStr = string.Empty;

            //* * * * * * * * * * * * * * * * * * * * 创建请求 * * * * * * * * * * * * * * * * * * * *
            HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(reqUrl);

            //* * * * * * * * * * * * * * * * * * * * 设置参数 * * * * * * * * * * * * * * * * * * * *
            webReq.Method = method;
            webReq.KeepAlive = true;
            webReq.Timeout = 2000;
            webReq.ContentType = "text/html";
            webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.4; InfoPath.2; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            
            //* * * * * * * * * * * * * * * * * * * * 判定是否需要代理 * * * * * * * * * * * * * * * * * * * *
            if (useProxy) webReq.Proxy = new WebProxy("10.19.110.32", 8080);
           
            //* * * * * * * * * * * * * * * * * * * * post方式传递参数 * * * * * * * * * * * * * * * * * * * *
            if (method == "POST")
            {
                byte[] bs = Encoding.ASCII.GetBytes(param);
                webReq.ContentLength = bs.Length;
                using (Stream reqStream = webReq.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);
                }
            }

            //* * * * * * * * * * * * * * * * * * * * 获取 response * * * * * * * * * * * * * * * * * * * *
            using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), code))
                {
                    resultStr = reader.ReadToEnd();
                }
                if (response != null) response.Close();
            }

            //* * * * * * * * * * * * * * * * * * * * 完成请求,输出返回信息 * * * * * * * * * * * * * * * * * * * *
            return resultStr;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值