参考: .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. Received status code 400 from server: Bad Request

    一.报错信息 FAILURE: Build failed with an exception.* What went wrong: A problem occurred configuring roo ...

  2. Could not GET “XXXXX.xml”Received status code 400

    Could not GET "XXXXX.xml"Received status code 400 from seerver :Bad Request 解决方案如下: **×××确 ...

  3. Received status code 400 from server: Bad Request解决方案

    Received status code 400 from server: Bad Request解决方案 Could not GET ' https://dl.google.com/dl/andro ...

  4. Request failed with status code 413 AxiosError: Request failed with status code 413 请求体过大报错

     在写一个富文本框 内容上传时 由于上传的文件 比如图片文件太大导致发请求的时候 报错413AxiosError: Request failed with status code 请求体过大 这里为了 ...

  5. nexus上传报Error code 400, Bad Request问题

    nexus仓库中默认设置为disable redeploy,不可以重复上传,所以第一次上传成功后,不能接受同名的jar包. 所以,要支持可以重复上传jar,必须在对应的仓库设置allow redepl ...

  6. vNext中ABP的Exception的Http中的Status Code

    对于ABP的文档中的说明,那个httpcode是不一定的,如果你用Volo.Abp.UserFriendlyException他返回的错误也有可能是401 403 500等,这对于前端来说就是个不确定 ...

  7. 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() 在请求 ...

  8. 服务器如何返回429状态,Instagram远程服务器返回一个错误:(429)UNKNOWN STATUS CODE...

    我正在使用我自己的instagram api开发一些新程序. 一切都与我工作正常,除了以下的用户脚本 我想按照我的用户ID 的名单,所以我用这个代码Instagram远程服务器返回一个错误:(429) ...

  9. 重定向Http status code 303 和 302

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

最新文章

  1. 轻量级mysql服务_Navicat连接阿里云轻量级应用服务器mysql
  2. java注释跳转方法,Java自定义注解实现Router跳转
  3. Java -- 注解 annotation
  4. python canvas画移动物体_如何实现Canvas图像的拖拽、点击等操作
  5. C++11 并发指南六(atomic 类型详解三 std::atomic (续))
  6. (86)FPGA读文件激励(readmemh)
  7. 跑路后再删库?思科前员工离职后恶意删库,损失达 240 万美元!
  8. python自学免费教程-python免费入门教程/求完整的Python入门教程
  9. Redis 3.0正式版发布,正式支持Redis集群
  10. 医学统计学基础——第一讲
  11. centos6 系统安装 system-config-kickstart 工具
  12. Android TV CTS 测试认证汇总
  13. VSDX Annotator for mac(Visio 绘图工具)
  14. sql怎么实现2个表连接_多表上SQL连接:概述和实现
  15. linux系统优化步骤,优化Linux系统的12个步骤
  16. 发送、抄送、密送、分别发送、回复、回复全部、转发的区别(一篇文章研究透彻)
  17. 洛谷 P3369 【模板】普通平衡树
  18. 怎么在视频中截取音频作为手机铃声?
  19. ResNet详解与分析
  20. 打造创业团队的执行力

热门文章

  1. 关于解决arduino SHT1x诸多问题
  2. 不惧困难,阿特拉斯机器人展示超强平衡能力
  3. 一名提高选手的数论之路(一)
  4. Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器
  5. PHP设计模式——抽象工厂模式
  6. Linux笔记1- 6 --文件
  7. (转)深入理解SP、LR和PC
  8. div+css背景渐变色代码示例
  9. spring获取bean的方案
  10. Lazy Load, 延迟加载图片的 jQuery 插件 - NeoEase