在我看来,大部分可以完成的事情HttpWebRequest/Response也可以在WebClient课堂上完成.我读到的地方WebClient是一个高级包装器WebRequest/Response.
到目前为止,我无法看到任何无法实现的功能HttpWebRequest/Response,也无法看到WebClientHttpWebRequest/Response将为您提供更多"细粒度"控件.
我何时应该使用WebClient HttpWebRequest/Response?何时?(显然,HttpWebRequest/Response是HTTP特定的.)
如果HttpWebRequest/Response是较低级别那么WebClient,HttpWebRequest/Response我能用不能实现的目标完成WebClient什么?
我发现以下asp.net代码在从数据库提供文件时非常有用:
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Run Code Online (Sandbox Code Playgroud)
这允许用户将文件保存到他们的计算机,然后决定如何使用它,而不是尝试使用该文件的浏览器.
使用内容处置响应标头还可以做些什么?
http httpresponse httpwebresponse content-disposition http-headers
我在一些响应中返回一个流,设置适当的内容类型标题.我正在寻找的行为是这样的:
如果浏览器能够呈现给定内容类型的内容,那么它应该在浏览器窗口中显示它.
如果浏览器不知道如何呈现内容,那么它应该显示saveas对话框,其中文件名应该是响应中提供的文件名.
如果我使用以下内容设置Content-Disposition标头的问题:
"attachment; filename ="myfile.txt""
浏览器将始终显示"保存"对话框.
如果我没有设置Content-Disposition,则saveas对话框中使用的文件名是url中的文件名,在我的情况下不起作用.
我也尝试将Content-Disposition设置为内联,但结果是一样的.
http httpresponse httpwebresponse content-disposition http-headers
我正在启动一个HttpWebRequest,然后检索它的响应.偶尔,我得到500(或至少5 ##)错误,但没有描述.我可以控制两个端点,并希望接收端获得更多信息.例如,我想将异常消息从服务器传递给客户端.这可能使用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 error than the webRequest.GetResponse() method
// throws an exception and all I get is "The remote server returned an error: (500)."
}
Run Code Online (Sandbox Code Playgroud)
任何有关这方面的帮助将非常感激.
我看到很多例子,但是他们都慢慢地将它们一次读入字节数组或256个字符.为什么?
将结果Stream值转换为可以解析它的字符串是不可取的?
我能找到的最接近的东西是,System.Net.Mime.MediaTypeNames但似乎没有一切(如json),因为它似乎更专注于电子邮件附件.
我正在调用第三方服务,当我要求回复时,它会抛出一个异常
"身份验证失败,因为远程方已关闭传输流异常".
我认为发送凭据存在问题.我甚至尝试过提供新的凭据.这是完整的代码
string get_url = "https://**.*******.com/com/******/webservices/public_webservice.cfc?wsdl&Method=CreateUser&SiteID=**&WSPassword=******&UserName=******";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(get_url);
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
request.Credentials = CredentialCache.DefaultCredentials;
//request.UseDefaultCredentials = false;
//request.Credentials = new System.Net.NetworkCredential("*****", "*****");
request.ContentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";
// Show the sent stream
//lbl_send_stream.Text = send_stream;
//lbl_send_stream.Text = get_url;
// Get UserId And LoginToken From Third Party DB
// ==============================================
//Exception gets throwed When code hits here
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud)
有什么方法可以将Fiddler捕获到使用.NET HttpWebRequest和HttpWebResponse捕获请求和响应吗?
我正在使用Facebook Graph Api并尝试获取用户数据.我正在发送用户访问令牌,如果此令牌过期或无效Facebook返回状态代码400并且此响应:
{
"error": {
"message": "Error validating access token: The session is invalid because the user logged out.",
"type": "OAuthException"
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我使用这个C#代码时:
try {
webResponse = webRequest.GetResponse(); // in case of status code 400 .NET throws WebException here
} catch (WebException ex) {
}
Run Code Online (Sandbox Code Playgroud)
如果状态代码为400,则.NET抛出WebException,并且在异常被捕获之后我webResponse就是null这样,所以我没有机会处理它.我想这样做是为了确保问题是在过期的令牌中,而不是在其他地方.
有办法吗?
谢谢.
如何循环通过从Windows Phone 7中的HttpWebResponse获取的WebHeaderCollection来获取密钥和值?我们尝试过Enumerator.Current; 有了这个,我们只得到钥匙,而不是价值.我们这样做是为了获得重定向的URL.
httpwebresponse ×10
.net ×4
c# ×4
http ×2
http-headers ×2
httpresponse ×2
asp.net ×1
content-type ×1
exception ×1
facebook ×1
fiddler ×1
mime-types ×1
string ×1
web-services ×1
webclient ×1