HttpWebRequest 返回BadRequest(400) 同时返回Response
今天用Fiddler分析安卓APP的一个登陆功能的时候,账号和密码错误会返回相应的消息,并且状态码是400。
正常用法:
/// <summary>
/// 读取返回的内容
/// </summary>
/// <param name="asyncresult"></param>
private void ReadCallBack(IAsyncResult asyncresult)
{
String result = String.Empty;
try
{
HttpWebRequest myrequest = (HttpWebRequest)asyncresult.AsyncState;
HttpWebResponse response = (HttpWebResponse)myrequest.EndGetResponse(asyncresult); //读取返回对象
Stream responseStream = response.GetResponseStream(); using (var render = new StreamReader(responseStream, Encoding.UTF8))
{
result = render.ReadToEnd();
render.Close();
}
}
catch (Exception ex)
{ Console.WriteLine("出现异常:" + ex.Message);
}
//处理返回消息
if (Handler != null)
Handler(result);
}
当执行GetResponseStream就抛出异常了,异常提示是BadRequest.
要想获取400返回的信息,需要对catch部分做特殊处理。
添加catch代码:
catch (Exception ex)
{
using (WebResponse response = e.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
using (Stream data = response.GetResponseStream()){
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
}
}
这样就可以获取到400返回的消息了。
stackoverflow问题链接:
http://stackoverflow.com/questions/11660947/httpwebrequest-getresponse-throws-bad-request-400-error
HttpWebRequest 返回BadRequest(400) 同时返回Response的更多相关文章
- WCF(远程服务器返回错误: 400 错误的请求)
类似相关问题有以下: WCF- restful接口 POST方式调用报错(远程服务器返回错误: 400 错误的请求) WCF Rest:不使用UriTemplate使用post方式传参解决HTTP40 ...
- post数据时报错:远程服务器返回错误: (400) 错误的请求。
网上查了多种方法,有不少说法,报400说是传的数据格式不对,最后的结论确实是数据格式不对. Content_Type为:application/json,配的数据格式有些麻烦,特别数多层,单层还好.例 ...
- 十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误
一.远程服务器返回错误: (400) 错误的请求错误 捕获异常查看具体错误 using Newtonsoft.Json; using System; using System.Collections. ...
- SharePoint 2013 Workflow Manager 1.0 远程服务器返回错误: (400) 错误的请求。 不支持查询字符串中的 api-version
环境: Windows Server 2012 R2 Standard SharePoint Server 2013 with sp1 通过Web 平台安装程序 5.0,已安装 Workflow Ma ...
- 前端如何获取http状态码400的返回值
axios.get("/check_mobile_and_sent_code",{withCredentials:true,params:{mobile:formInline.mo ...
- Web API-如何将Controller的返回值转换成HTTP response消息
https://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization https://co ...
- react-native-image-picker在IOS上总是返回”Can’t find variable:response”的错误?
环境: react-native: 0.41.2 react-native-image-picker: 0.26.2 xcode 8.2.1 iphone 6 根据官方教程(https://githu ...
- C# HttpWebRequest请求远程地址获取返回消息
HttpWebRequest请求远程地址获取返回消息 /// <summary> /// 请求远程Api获取响应返回字符串 /// </summary> /// <par ...
- 在一个exe文件中查找指定内容,找到则返回起始位置, 否则返回0
//在一个exe文件中查找指定内容,找到则返回起始位置, 否则返回0//如果某字符串, 直接传入字符串进来//如果要查找16进制,则用如下格式传参进来: #$1A#$2A#$3A function F ...
随机推荐
- 在VS2013下如何配置DirectX SDK的开发环境_百度经验
jpg改rar
- Excel 信息对比_数组版
Sub LOOKUP_UChur() Dim i As Long '=== sourceWorksheet = 数据源表名称 Dim sourceWorksheet As Worksheet Dim ...
- fly
购物车飞入效果 核心: 1,购物车与飞入圆点(或者图标)的定位关系 完整源码: <!doctype html> <html lang="zh"> <h ...
- index封装
就是求元素在父级当中的位置: 思路: <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- c#基础 第四讲
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- Hibernate中双向的一对多关系
何为双向,双向的意思就是你我之间可以互相通信(customer(1)和order(n)) 也就是说customer可以访问order,order也可以访问customer 二者构成了双向的关系 在Hi ...
- poj3233—Matrix Power Series
题目链接:http://poj.org/problem?id=3233 题目意思:给一个矩阵n*n的矩阵A和一个k,求一个式子 S = A + A2 + A3 + … + Ak. 这个需要用到等比数列 ...
- web容器与web服务器
apache.nginx 这类是web服务器tomcat.jboss.Kestrel(asp.net core) 这类是web容器而iis.jexus 两者都是 apache.nginx 是不能直接跑 ...
- Spark源码分析 – Checkpoint
CP的步骤 1. 首先如果RDD需要CP, 调用RDD.checkpoint()来mark 注释说了, 这个需要在Job被执行前被mark, 原因后面看, 并且最好选择persist这个RDD, 否则 ...
- 【转】dd命令详解及利用dd测试磁盘性能
dd命令详解及利用dd测试磁盘性能 linux下dd命令详解 名称: dd 使用权限: 所有使用者 manpage 定义: convert and copy a file 使用方式: dd [op ...