Tag: httpwebresponse

当.NET抛出WebException((400)Bad Request)时如何处理WebResponse?

我正在使用Facebook Graph Api并试图获取用户数据。 我发送用户访问令牌,万一这个令牌过期或无效的Facebook返回状态代码400和这个回应: { "error": { "message": "Error validating access token: The session is invalid because the user logged out.", "type": "OAuthException" } } 问题是,当我使用这个C#代码: try { webResponse = webRequest.GetResponse(); // in case of status code 400 .NET throws WebException here } catch (WebException ex) { } 如果状态代码是400 .NET抛出WebException,并且在exception被捕获后我的webResponse为null ,所以我没有机会处理它。 我想这样做,以确保问题是在过期的令牌,而不是其他地方。 有没有办法做到这一点? 谢谢。

获取使用HttpWebRequest / HttpWebResponse进行的HTTP请求和响应以显示在Fiddler中

有没有什么办法可以把Fiddler挂起来捕获使用.NET HttpWebRequest和HttpWebResponse所做的请求和响应?

HttpWebResponse(“text / plain”,“application / octet-stream”等)上的ContentType属性是否有枚举?

我能find的最接近的是System.Net.Mime.MediaTypeNames但似乎没有一切(如json),因为它似乎更专注于电子邮件附件。

如何获取HttpWebRequest.GetResponse()失败时的错误信息

我正在启动一个HttpWebRequest,然后检索它的响应。 偶尔,我得到一个500(或至less5个##)的错误,但没有说明。 我可以控制两个端点,并希望接收端获得更多信息。 例如,我想将exception消息从服务器传递给客户端。 这可能使用HttpWebRequest和HttpWebResponse? 码: try { HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest; webRequest.Method = WebRequestMethods.Http.Get; webRequest.Credentials = new NetworkCredential(Username, Password); webRequest.ContentType = "application/x-www-form-urlencoded"; using(HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse) { if(response.StatusCode == HttpStatusCode.OK) { // Do stuff with response.GetResponseStream(); } } } catch(Exception ex) { ShowError(ex); // if the server returns a 500 […]

如何转换WebResponse.GetResponseStream返回string?

我看到很多例子,但是所有这些例子都是一次慢慢读入字节数组或者256个字符。 为什么? 仅仅将结果的Stream值转换为一个可以parsing它的string是不可取的?