• C# WebRequest WebResponse的使用


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;


    namespace WebExam
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                WebRequest request = WebRequest.Create("http://www.163.com");


                //request.BeginGetResponse(new AsyncCallback(OnResponse), request);异步页面请求


                NetworkCredential cred = new NetworkCredential("admin", "admin");
                //request.Credentials = cred;//验证


                //WebProxy wp = new WebProxy("192.168.1.100", true);//代理
                //wp.Credentials = cred;


                HttpWebRequest httpRequest = (HttpWebRequest)request;
                listBox1.Items.Add("Request time out(ms):" + request.Timeout);
                listBox1.Items.Add("Request keep alive:" + httpRequest.KeepAlive);
                listBox1.Items.Add("Request AllowAutoRedirect:" + httpRequest.AllowAutoRedirect);


                WebResponse response = request.GetResponse();
                WebHeaderCollection headers = response.Headers;
                for (int i = 0; i < headers.Count; i++)
                {
                    listBox1.Items.Add(string.Format("{0}:{1}",headers.GetKey(i),headers[i]));
                }
            }


            //异步页面请求
            //private void OnResponse(IAsyncResult ar)
            //{
            //    WebRequest request = (WebRequest)ar.AsyncState;
            //    WebResponse response = request.EndGetResponse(ar);
            //    //read response
            //}
        }
    }
  • 相关阅读:
    通过mysql命令行理解mysql
    yii2.0框架安装心得
    浅谈我的编程之路——感谢引领我的leader
    谈谈我的编程之路---WAMP(四)
    谈谈我的编程之路---WAMP(三)
    Sql语句优化之explan分析案例
    mysql技术内幕笔记
    Sql语句优化
    linux变量
    sql查询慢优化
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434768.html
Copyright © 2020-2023  润新知