我有一个.net应用程序,它有一个WebRequest,它给POST添加了多次相同的密钥,从而使它成为PHP,Java Servlets等眼中的数组.我想把它重写为使用WebClient,但如果我多次使用相同的键调用WebClient的QueryString.Add(),它只是附加新值,使用逗号分隔的单个值而不是值数组.
我使用WebClient的UploadFile()发布我的请求,因为除了这些元数据,我还想发布一个文件.
如何使用WebClient发布值数组而不是单个值(逗号分隔值)?
干杯
聂
假设我有这个函数,可以从主线程中多次调用.每次调用它时,我都会创建一个WebClient对象来异步下载一些数据.
我的问题......这样做安全吗?WebClient事件被调用后是否释放了对象?如果它不会自动释放,我不想继续分配内存.
我的应用程序适用于带Silverlight的WP7.
谢谢!
void DownloadData(string cURL)
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted +=
new System.Net.DownloadStringCompletedEventHandler(
webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(cURL));
}
static void webClient_DownloadStringCompleted(object sender,
System.Net.DownloadStringCompletedEventArgs e)
{
...
}
Run Code Online (Sandbox Code Playgroud) 我有一个工作的PowerShell 3.0脚本,它利用Invoke-WebRequest发布表单并保存相应的原始HTML输出.不幸的是,该脚本不适用于仅使用PowerShell 2.0的工作站.
我希望有人可以帮助转换下面的脚本以使用PowerShell 2.0(无需安装PS模块,软件包或升级PowerShell).看起来我需要使用.NET webclient,但我还没有足够的经验来做到这一点.一个工作示例(基于我的脚本)将非常感谢!
PS:我sls -pattern用来过滤掉原始HTML输出中的内容.如果有一种更可靠的方法来识别原始输出中的某些项目,我不介意看一个例子.
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET CLR 1.1.4322; InfoPath.3; MS-RTC LM 8; .NET4.0E)'
$r=Invoke-WebRequest -Uri 'http://web.ourwebserver.com/test.aspx' -UseDefaultCredentials -SessionVariable WebSession
$form = $r.Forms[0]
$fields = Invoke-WebRequest -Uri 'http://web.ourwebserver.com/test.aspx' -WebSession $WebSession | select -ExpandProperty inputfields | select name, value
$viewstate = $fields | ?{$_.name -match "VIEWSTATE"} | select -ExpandProperty value …Run Code Online (Sandbox Code Playgroud) 我在我的Form构造函数中,在InitializeComponent之后有以下代码:
using (WebClient client = new WebClient())
{
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
client.DownloadDataAsync("http://example.com/version.txt");
}
Run Code Online (Sandbox Code Playgroud)
当我启动表单时,UI不会出现,直到引发client_DownloadDataCompleted.client_DownloadDataCompleted方法为空,因此没有问题.
我做错了什么?如何在不冻结UI的情况下做到这一点?
谢谢你的时间.
最好的祝福.
完整代码:
Program.cs中
using System;
using System.Windows.Forms;
namespace Lala
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Run Code Online (Sandbox Code Playgroud)
Form1.cs的
using System;
using System.Net;
using System.Windows.Forms;
namespace Lala
{
public partial class Form1 : Form
{
WebClient client = new WebClient();
public Form1()
{ …Run Code Online (Sandbox Code Playgroud) 我想创建自己的自定义HTTP请求.WebClient类非常酷,但它会自动创建HTTP请求.我想我需要创建一个到Web服务器的网络连接,并通过该流传递我的数据,但我不熟悉支持这种事情的库类.
(上下文,我正在为我正在教授的Web编程课程编写一些代码.我希望我的学生能够理解HTTP"黑盒子"中发生的事情的基础知识.)
是否可以直接从浏览器向服务提供商发送SOAP请求?然后在javascript中解析输出以显示结果?
举例来说,如果我有一个SOAP请求这样的:
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)
那么我可以通过点击网页上的链接获得"IBM股票价格"吗?并在xml处理后显示结果.
我试着像这样下载文件:
WebClient _downloadClient = new WebClient();
_downloadClient.DownloadFileCompleted += DownloadFileCompleted;
_downloadClient.DownloadFileAsync(current.url, _filename);
// ...
Run Code Online (Sandbox Code Playgroud)
下载后我需要用下载文件启动另一个进程,我尝试使用DownloadFileCompleted 事件.
void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
throw e.Error;
}
if (!_downloadFileVersion.Any())
{
complited = true;
}
DownloadFile();
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法知道下载文件的名称,AsyncCompletedEventArgs我自己做了
public class DownloadCompliteEventArgs: EventArgs
{
private string _fileName;
public string fileName
{
get
{
return _fileName;
}
set
{
_fileName = value;
}
}
public DownloadCompliteEventArgs(string name)
{
fileName = name;
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法理解如何调用我的活动 DownloadFileCompleted
对不起,如果这是一个很好的问题
可能重复:
加载EXE文件并使用C#从内存运行它
我正在使用WebClient类从Web服务器下载.exe.有没有办法可以运行.exe而不先将其保存到磁盘?
为了完整起见,让我告诉你到目前为止我所拥有的.
这是我用来开始下载的代码:
WebClient webClient = new WebClient();
webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webClient_DownloadDataCompleted);
webClient.DownloadDataAsync(new Uri("http://www.somewebsite.com/calc.exe"));
Run Code Online (Sandbox Code Playgroud)
在(webClient_DownloadDataCompleted)方法中,我只需从参数e中获取字节:
private void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
Byte[] downloadedData = e.Result;
// how to run this like a .exe?
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
如何从UWP中的URL下载数据?我习惯使用WebClient的DownloadData方法,但不能再使用了.
我试图用字符串保存网站的html.该网站具有国际字符(ę,ś,ć,...),即使我将编码设置为UTF-8(对应于网站字符集),它们也不会保存到字符串中.
这是我的代码:
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
string htmlCode = client.DownloadString(http://www.filmweb.pl/Mroczne.Widmo);
}
Run Code Online (Sandbox Code Playgroud)
当我将"htmlCode"打印到控制台时,即使在原始HTML中它们被正确显示,国际字符也不会正确显示.
任何帮助表示赞赏.