模拟http 提示数据 POST 和 GET

#region 内部使用 HTTP GET模式提交数据 【HttpPostData】
    /// <summary>
    /// 内部使用 HTTP GET模式提交数据 【HttpPostData】
    /// </summary>
    /// <param name="url">完整的URL地址</param>+
    /// <param name="requestBytes">提交参数</param>
    /// <returns>返回数据</returns>
    private string HttpPostData(string url, string data)
    {
        try
        {
            Stream newStream = null;
            WebRequest myReq = WebRequest.Create(url);
            byte[] byte1 = UTF8Encoding.Default.GetBytes(data);
            myReq.Method = "POST";
            myReq.ContentType = "application/x-www-form-urlencoded";
            myReq.ContentLength = byte1.Length;
            newStream = myReq.GetRequestStream();
            newStream.Write(byte1, 0, byte1.Length);
            WebResponse myResp = myReq.GetResponse();
            Stream resStream = myResp.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, System.Text.Encoding.GetEncoding("UTF-8"));
            return sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            LogHelper.WriteException("【HttpPostData】异常url【" + url + "】", ex);
            return "";
        }


    }
    #endregion

 

 

 

 

 

 

 

 

 

#region 内部使用 HTTP GET模式提交数据 【HttpGetData】
    /// <summary>
    /// 内部使用 HTTP GET模式提交数据
    /// 如:http://localhost:8080/pastf.aspx?username=asb&passpod=123;
    /// </summary>
    /// <param name="url">提交地址(需要加参数 用小写)</param>
    /// <returns></returns>
    private string HttpGetData(string url)
    {
        Uri uri = new Uri(url);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        string page;
        try
        {

            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;

            request.Method = "GET";

            request.ContentType = "application/x-www-form-urlencoded";

            request.Proxy = System.Net.WebProxy.GetDefaultProxy();

 

            //allow auto redirects from redirect headers
            request.AllowAutoRedirect = true;

 

            //maximum of 10 auto redirects
            request.MaximumAutomaticRedirections = 10;

 

            //30 second timeout for request
            request.Timeout = (int)new TimeSpan(0, 3, 0).TotalMilliseconds;

 

            //give the crawler a name.
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";


            //request.UserAgent="Mozilla/3.0 (compatible; My Browser/1.0)";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();


            Stream responseStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);

            page = readStream.ReadToEnd();
        }
        catch (Exception ex)
        {
            LogHelper.WriteException("【HttpGetData】异常url【" + url + "】", ex);
            page = "";
        }
        return page;

    }
    #endregion

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值