参考: .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);
                    }
                }
            }
        }
    }
}

HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned的更多相关文章

  1. 解决 android studio 出现:"AndroidStudio:Could not GET 'https://dl.google.com Received status code 400 from server: Bad Request"问题

    一.android studio 编译项目时出现"AndroidStudio:Could not GET 'https://dl.google.com Received status cod ...

  2. Android Studio Gradle build 报错:Received status code 400 from server: Bad Request

    错误提示如下 Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.2/ ...

  3. Failed to load resource: the server responsed with a status of 400 (Bad Request)

    浏览器报错:Failed to load resource: the server responsed with a status of 400 (Bad Request) ajax请求失败,一般情况 ...

  4. 【解决了一个小问题】gin框架中出现如下错误:"[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 500"

    POST到数据到一条gin框架的接口后,客户端收到400错误,并且返回了业务中返回的"decode json fail". 关键代码是: func report(c *gin.Co ...

  5. HTTP 1.0 Status Code Definitions

    part of Hypertext Transfer Protocol -- HTTP/1.1RFC 2616 Fielding, et al. 10 Status Code Definitions ...

  6. System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse()

    System.Net.WebException: The operation has timed out  at System.Net.HttpWebRequest.GetResponse() 在请求 ...

  7. 重定向Http status code 303 和 302

    http 302 http 303 Http 302 302是一个普通的重定向代码.直观的看来是,请求者(浏览器或者模拟http请求)发起一个请求,然后服务端重定向到另一个地址.而事实上,服务端仅仅是 ...

  8. http status code

    属于转载 http status code:200:成功,服务器已成功处理了请求,通常这表示服务器提供了请求的网页 404:未找到,服务器未找到 201-206都表示服务器成功处理了请求的状态代码,说 ...

  9. C#、JAVA操作Hadoop(HDFS、Map/Reduce)真实过程概述。组件、源码下载。无法解决:Response status code does not indicate success: 500。

    一.Hadoop环境配置概述 三台虚拟机,操作系统为:Ubuntu 16.04. Hadoop版本:2.7.2 NameNode:192.168.72.132 DataNode:192.168.72. ...

随机推荐

  1. Elasticsearch .Net Client NEST 索引DataSet数据

    NEST 索引DataSet数据,先序列化然后转成dynamic 类型进行索引: /// <summary> /// 索引dataset /// </summary> /// ...

  2. 好用的log

    Log.getStackTraceString(new Throwable())

  3. hdu 3450 Counting Sequences

    /* n*n暴力 这个很好想 */ #include<cstdio> #define maxn 100010 #define mod 9901 using namespace std; i ...

  4. js时间字符串转Date对象

    var DATE_REGEXP = new RegExp("(\\d{4})-(\\d{2})-(\\d{2})([T\\s](\\d{2}):(\\d{2}):(\\d{2})(\\.(\ ...

  5. 前台之boostrap

    这个网址有些东西不错

  6. 发布MVC IIS 报错未能加载文件或程序集“System.Web.Http.WebHost

    未能加载文件或程序集“System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e3 ...

  7. HTML5 <Audio/>标签Api整理(二)

    1.实例2: 相对较完整 Html代码: <style> #volumeSlider .slider-selection { background:#bababa; } </styl ...

  8. WPF TextSelection获取选中部分内容

    一.简单实例 //TextSelect继承自TextRange TextSelection selection = richTextBox.Selection; //1.获取选中内容 string r ...

  9. 安卓工程修改包名后 Failed to find provider info for...问题

    安卓工程修改包名后 Failed to find provider info for com.android...provider问题 原因: 1. 多处含包名文件须同时更改 2. Manifest里 ...

  10. EF6.0+Mysql的问题

    最近在项目中使用EF for Mysql的时候遇到一个问题 public OrderManage GetOrders(OrderSearchCriteria criteria) { using (va ...