尽管找到了例子,但我在这方面遇到了一些麻烦。我认为这可能是一个编码问题,但我不确定。我正在尝试以编程方式从使用 cookie 的 https 服务器下载文件(因此我使用 httpwebrequest)。我正在调试打印要检查的流的容量,但输出[原始]文件看起来不同。尝试过其他编码方式均无效。
代码:
Sub downloadzip(strURL As String, strDestDir As String)
Dim request As HttpWebRequest
Dim response As HttpWebResponse
request = Net.HttpWebRequest.Create(strURL)
request.UserAgent = strUserAgent
request.Method = "GET"
request.CookieContainer = cookieJar
response = request.GetResponse()
If response.ContentType = "application/zip" Then
Debug.WriteLine("Is Zip")
Else
Debug.WriteLine("Is NOT Zip: is " + response.ContentType.ToString)
Exit Sub
End If
Dim intLen As Int64 = response.ContentLength
Debug.WriteLine("response length: " + intLen.ToString)
Using srStreamRemote As StreamReader = New StreamReader(response.GetResponseStream(), Encoding.Default)
'Using ms As New MemoryStream(intLen) …Run Code Online (Sandbox Code Playgroud) 我使用webbrowser控件打开一个网站,然后将cookie保存在cookieContainer中,稍后使用HTTPwebrequest处理前向浏览页面等.
问题出现了,当我进行搜索并返回100页时,在第一页上,它保存了一个名为ABC的cookie,我将其添加到cookiecontainer并移至下一页,在第二页上再次使用相同的Cookie命名: ABC有一些价值,但现在我在cookiecontainer中有两个相同的cookie,当我移动到下一页时,它不起作用,因为它采取了第一个混乱的东西.
怎么解决这个?
HttpWEBREQUEST功能:
public string getHtmlCookies(string url)
{
string responseData = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "*/*";
request.AllowAutoRedirect = true;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
request.Timeout = 30000;
request.Method = "GET";
request.CookieContainer = yummycookies;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
foreach (Cookie cookie in response.Cookies)
{
string name = string.Empty;
name = cookie.Name;
string value = cookie.Value;
string path = "/";
string domain = …Run Code Online (Sandbox Code Playgroud) c# cookies httpwebrequest httpwebresponse visual-studio-2010
处理我试图通过HttpWebResponse在我的应用程序中下载的数据时遇到了很大的问题.我的代码看起来像这样:
myWebRequest.Timeout = 10000;
using (HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse())
{
using (Stream ReceiveStream = myWebResponse.GetResponseStream())
{
Encoding encode = Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(ReceiveStream, encode);
// Read 1024 characters at a time.
Char[] read = new Char[1024];
int count = readStream.Read(read, 0, 1024);
int break_counter = 0;
while (count > 0 && break_counter < 10000)
{
String str = new String(read, 0, count);
buffer += str;
count = readStream.Read(read, 0, 1024);
break_counter++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码在分离的线程中的几个实例中运行,因此调试有点困难.问题是这种方法卡住了,我把它归咎于与数据的连接不良.
正如您所看到的,我已经设置了超时,并且希望代码在超时时间到期后才会终止.它不是!至少不是所有的时间.有时我得到一个WebException/Timeout但有几次它只是卡住了.
什么是超时?什么时候打电话?让我们说HttpWebResponse开始接收数据,但它在传输过程中卡在某处.我有超时吗?对我来说,看起来我没有,因为我的应用程序也被卡住了,并且没有引发超时异常.
我可以做些什么来修补此问题,或者如何获得有关此处出现问题的更多信息?
我只是想知道这是否可行 - 我知道如何下载文件,但是如何只下载文件的前100KB?
HttpWebResponse.LastModified准确吗?它总是存在吗?我的项目是创建一种专注的Web爬虫,如果我将使用资源的哈希值或仅使用HttpWebResponse.LastModified属性来检查资源的"新鲜度",我就会陷入困境.
使用哈希值意味着每次检查时都会流式传输资源.这对整体表现有很大影响.
如果我只是检查HttpWebResponse.LastModified,它是否准确?
我们目前在阅读我们过去没有遇到任何问题的ResponseStream时遇到问题.自从昨晚将.NET 4.0 Framwework添加到我们的服务器并分配IIS以使用新框架后,我们在尝试使用以下语句(responseStream = httpResponse.GetResponseStream();)尝试读取responseStream时遇到了一些不同的异常.到目前为止,一切都完美无缺.所以,我正在寻找有关如何从响应中读取的更改/改进.我已经粘贴了我们正在使用的以下代码以及我们遇到的例外情况.
.NET Framework 4.0 Windows Server 2003
HttpWebResponse httpResponse;
Stream responseStream;
//Accept All Certificate Policy
ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(theUrl));
httpRequest.Method = "POST";
httpRequest.KeepAlive = false;
httpRequest.Timeout = timeOut;
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
responseStream = httpResponse.GetResponseStream();
}
Run Code Online (Sandbox Code Playgroud)
'httpResponse.GetResponseStream().Length'抛出类型'System.NotSupportedException'的异常long {System.NotSupportedException}
'httpResponse.GetResponseStream().Position'抛出类型'System.NotSupportedException'的异常long {System.NotSupportedException}
{"此流不支持搜索操作."} System.SystemException {System.NotSupportedException}
问候,
麦克风
我怎样才能读取一些字节并断开连接?我使用这样的代码
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
using (Stream sm = resp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(sm, Encoding.Default))
{
sr.Read();
sr.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它等待流的结束
通过最近涉及WebResponse的一些代码调试后,我发现我遇到的问题是我在发出另一个之前没有正确处理WebResponse.因为WebResponse需要被转换为IDisposable以便实际调用dispose(或者你可以使用"using"来实现相同的目标),所以我误入歧途.
所以我的问题是:
1)Microsoft用什么来实现这一目标?
IDisposable是一个接口,因此是公共的,但WebResponse以某种方式改变了根据MSDN doumentation保护的访问修饰符.我认为这是不可能的.
2)以这种方式隐藏处置有什么好处?
为什么不让webResponse.Dispose()有效?
我目前正在使用此脚本来获取HTTP响应标头.
public static List<string> GetHttpResponseHeaders(string url)
{
List<string> headers = new List<string>();
WebRequest webRequest = HttpWebRequest.Create(url);
using (WebResponse webResponse = webRequest.GetResponse())
{
headers.Add("Status Code: " + (int) ((HttpWebResponse) webResponse).StatusCode);
}
return headers;
}
Run Code Online (Sandbox Code Playgroud)
具体来说,Status Code:就是我感兴趣的内容.有了这样说,似乎StatusCode()实际上并没有返回"状态代码",并且在成功的请求中,它只返回一个OK而不是一个200.
有没有办法强制它返回实际代码而不是描述?
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
HttpStatusCode statusCode = response.StatusCode;
Run Code Online (Sandbox Code Playgroud)
在此代码中statusCode.ToString()返回例如"BadRequest"但我需要"错误请求"
我看到了arcticles response.ReasonPhrase,但这不是我需要的东西,它不受支持HttpWebResponse,只有HttpResponseMessage来自的支持HttpClient
另一个反对Regex.Replace解决方案的例子:(414)RequestUriTooLong- >Request-Uri Too Long
httpwebresponse ×10
c# ×9
.net ×3
.net-4.0 ×1
cookies ×1
dotnetzip ×1
http ×1
networking ×1
timeout ×1
vb.net ×1
webresponse ×1