今天用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/692342/net-httpwebrequest-getresponse-raises-exception-when-http-status-code-400-ba

http://stackoverflow.com/questions/11660947/httpwebrequest-getresponse-throws-bad-request-400-error

HttpWebRequest 返回BadRequest(400) 同时返回Response的更多相关文章

  1. WCF(远程服务器返回错误: 400 错误的请求)

    类似相关问题有以下: WCF- restful接口 POST方式调用报错(远程服务器返回错误: 400 错误的请求) WCF Rest:不使用UriTemplate使用post方式传参解决HTTP40 ...

  2. post数据时报错:远程服务器返回错误: (400) 错误的请求。

    网上查了多种方法,有不少说法,报400说是传的数据格式不对,最后的结论确实是数据格式不对. Content_Type为:application/json,配的数据格式有些麻烦,特别数多层,单层还好.例 ...

  3. 十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误

    一.远程服务器返回错误: (400) 错误的请求错误 捕获异常查看具体错误 using Newtonsoft.Json; using System; using System.Collections. ...

  4. SharePoint 2013 Workflow Manager 1.0 远程服务器返回错误: (400) 错误的请求。 不支持查询字符串中的 api-version

    环境: Windows Server 2012 R2 Standard SharePoint Server 2013 with sp1 通过Web 平台安装程序 5.0,已安装 Workflow Ma ...

  5. 前端如何获取http状态码400的返回值

    axios.get("/check_mobile_and_sent_code",{withCredentials:true,params:{mobile:formInline.mo ...

  6. Web API-如何将Controller的返回值转换成HTTP response消息

    https://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization https://co ...

  7. 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 ...

  8. C# HttpWebRequest请求远程地址获取返回消息

    HttpWebRequest请求远程地址获取返回消息 /// <summary> /// 请求远程Api获取响应返回字符串 /// </summary> /// <par ...

  9. 在一个exe文件中查找指定内容,找到则返回起始位置, 否则返回0

    //在一个exe文件中查找指定内容,找到则返回起始位置, 否则返回0//如果某字符串, 直接传入字符串进来//如果要查找16进制,则用如下格式传参进来: #$1A#$2A#$3A function F ...

随机推荐

  1. webstorm配置内存参数,解决卡顿

    找到WebStorm.exe.vmoptions这个文件,路径如下webstorm安装主目录>bin>WebStorm.exe.vmoptions更改为第二行:-Xms526m第三行:-X ...

  2. win7显示方向旋转快捷键禁用及图形属性打开方法

    方法/步骤   1 首先在桌面右键→打开[图形属性],如果没有,请看步骤2.如果有,直接进入步骤3 步骤阅读 2 为了美化桌面右键,往往会把桌面右键中的图形选项隐藏掉,此时,我们可以通过[控制面板]打 ...

  3. URL地址传值型多条件搜索JS

    function ResetSearchVal(objArray) { var strUrl = location.href; ; i < objArray.length; i++) { var ...

  4. poj_3628 动态规划

    题目大意 有N个数字,大小为a[i], 给定一个数S,用这N个数中的某些数加起来使得结果sum>= S,且sum-S最小,求该最小的sum-S值. 题目分析 题意中可知,这N个数字的和肯定大于S ...

  5. jQuery中的find()与filter()

    这是jQuery里常用的2个方法.他们2者功能是完全不同的. <div class="css"> <p class="rain">测试1 ...

  6. 160309、Spring AOP操作action时无法注入,报空指针错误

    今天帮同事看个问题,action注入失败,代码没问题,主要是stuts2权限移交的问题,特此记录一下 Spring AOP操作action时无法注入,报NullPointer异常 当使用Spring ...

  7. C#批量入库

    public static void BulkCopyToDB(DataTable dt, string conn, string tableName, out string msg) { msg = ...

  8. 阿里云OSS分片上传DEMO

    分片传输规则 1.不能超过10000片,2.每片必须大于100KB using System; using System.Collections.Generic; using System.Compo ...

  9. iOS如何让主界面不显示NavigationBar

    这个问题曾经困扰过我.现在我给出正解.- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated]; [self ...

  10. If the parts of an organization (e.g., teams, departments, or subdivisions) do not closely reflect the essential parts of the product, or if the relationship between organizations do not reflect the r

    https://en.wikipedia.org/wiki/Conway%27s_law