如何自动点击"你想打开或保存&QUOT?; [英] how to automatically click on "do you want to open or save?"

查看:142
本文介绍了如何自动点击"你想打开或保存&QUOT?;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个C#或硒的解决方案如下:

I am wondering if there is a c# or selenium solution to the following:

我使用的硒下载文件了一个网络服务器。

I am using selenium to download a file off a webserver.

可惜IE9没有办法禁用此弹出式屏幕。

unfortunately in IE9 there is no way to disable this popup screen.

是有点击保存按钮?

推荐答案

客户端

WebClient client = new WebClient();
byte[] file = client.DownloadData("http://domain.com/default.aspx");
File.WriteAllBytes("helloworld.txt", file);






服务器

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (MemoryStream memory = new MemoryStream())
        using (StreamWriter writer = new StreamWriter(memory))
        {
            // The bytes written can be anything, does not have to be text
            writer.WriteLine("hello, world");
            writer.Flush();
            Response.BinaryWrite(memory.ToArray());
        }

        Response.AddHeader("Content-Disposition", "Attachment; filename=helloworld.txt");
        Response.AddHeader("Content-Type", "application/octet-stream");
        Response.End();
    }
}



你可以用上面的例子中不仅看到了,我不知道该文件的物理位置,但没有物理位置!这是我刚写到内存,然后写了原始字节的回应,但WebClient.DownloadData()将仍然能够下载字节都是一样的。它不关心那里的字节从何而来。 。希望这有助于

As you can see with the above example, not only do I not know the physical location of the file, but there is no physical location! It is something I just wrote to memory and then wrote the raw bytes to the response, but WebClient.DownloadData() will still be able to download the bytes all the same. It doesn't care where the bytes come from. Hope this helps.

其他信息:

一些进一步的背景资料进一步解释为什么上述作品。

Some further background information further explaining why the above works.

一个浏览器的主要任务是发送HTTP请求和处理响应。幸运的是,他们处理了很多对我们繁重的工作。显示一个简单的网页包括发送HTTP-GET到服务器并接收字节背在身上的响应,这些字节解码成文字,并分析该文本呈现的HTML网页。它知道来处理这样的反应,因为它的text / html的Content-Type头。虽然这是做什么的大部分时间里,浏览器还可以处理其他MIME类型,如果Content-Type是text / plain的,将解码字节,只显示他们没有试图解析它。文/ XML通常会允许你折叠和展开XML节点等再次,这是所有依赖于浏览器的编程方式来处理粒子MIME类型。

A browser's primary job is to send HTTP requests and handle the response. Fortunately, they handle a lot of the grunt work for us. Showing a simple web page involves sending an HTTP-GET to the server and receiving bytes back in the response body, decoding those bytes into text, and parsing that text to render an HTML web page. It knows to handle the response like that because it has a Content-Type header of Text/HTML. While this is what it does most of the time, browsers can also handle other MIME types, if the Content-Type is text/plain it will decode the bytes and just display them without trying to parse it. Text/XML will usually allow you to collapse and expand XML nodes, etc. Again this is all dependent on how the browser is programmed to handle that particle MIME type.

当你得到一个另存为在浏览器对话框,这是一个简单的处理了内容处置的响应浏览器的方式:附件头。这头告诉浏览器不要尝试呈现或显示的内容,而是将其下载作为附件来代替。

When you get a Save As dialog box in a browser, that is simply the browser's way of handling a response with a Content-Disposition: Attachment header. This header tells the browser not to try to render or display the content, but to download it as an attachment instead.

当您使用Web客户端/ HttpWebRequest的班,你基本上是写自己的微型浏览器,但是的MIME类型/ HTTP报头是如何处理的实现是完全由你。这可以让您保存从内容处置响应字节(或与此有关的任何回应),而不使用打开或保存对话框提示。

When you use the WebClient / HttpWebRequest classes, you are essentially writing your own miniature browser, however the implementation of how MIME types / HTTP headers are handled is entirely up to you. And this can allow you to save the bytes from a Content-Disposition response (or any response for that matter) without prompting with a Open or Save dialog box.

这篇关于如何自动点击"你想打开或保存&QUOT?;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆