• HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned


    参考: .Net HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned
    C# code

    using System;
    using System.IO;
    using System.Net;
     
    public class Test
    {
        static void Main()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://csharpindepth.com/asd");
            try
            {
                using (Stream data = request.GetResponse().GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(data))
                    {
                        string text = reader.ReadToEnd();
                        Console.WriteLine(text);
                    }
                }
            }
            catch (WebException ex)
            {
                HttpWebResponse response = (HttpWebResponse) ex.response;
                Console.WriteLine("Error code: {0}", response.StatusCode);
                if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    using (Stream data = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(data))
                        {
                            string text = reader.ReadToEnd();
                            Console.WriteLine(text);
                        }
                    }
                }
            }
        }
    }

  • 相关阅读:
    一个漂亮的lazarus做的pagecontrol
    预测,阿里盒子必将失败!
    sex在软件开发中的运用--SIX技术
    糟糕的@@identity,SCOPE_IDENTITY ,IDENT_CURRENT
    Delphi、Lazarus保留字、关键字详解
    糟糕的界面设计
    Firebird存储过程--更加人性化的设计
    lazarus的动态方法和虚拟方法
    用户行为导向的交互设计
    Javascript的一个怪现象
  • 原文地址:https://www.cnblogs.com/micro-chen/p/6214331.html
Copyright © 2020-2023  润新知