看看代码:
using (var client = new WebClient())
{
using (var stream = client.OpenWrite("http://localhost/", "POST"))
{
stream.Write(post, 0, post.Length);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我如何读取HTTP输出?
我正在努力处理两种不同WebException的问题.
基本上他们在打电话后处理 WebClient.DownloadFile(string address, string fileName)
AFAIK,到目前为止我必须处理两个,两个WebException:
可能会有更多,但这是我迄今为止发现最重要的内容.
那么我应该如何处理这个问题,因为它们都是,WebException但我想以不同的方式处理每个案例.
这是我到目前为止:
try
{
using (var client = new WebClient())
{
client.DownloadFile("...");
}
}
catch(InvalidOperationException ioEx)
{
if (ioEx is WebException)
{
if (ioEx.Message.Contains("404")
{
//handle 404
}
if (ioEx.Message.Contains("remote name could not")
{
//handle file doesn't exist
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在检查消息以查看它是什么类型的WebException.我会假设有更好或更精确的方法来做到这一点?
谢谢
我希望VB.net WebClient能够记住cookie.
我已经搜索并尝试了许多重载类.
我想通过POST登录到一个网站,然后POST到另一个页面并获取其内容,同时仍保留我的会话.
这是否可以使用VB.net而不使用WebBrowser控件?
我尝试过Chilkat.HTTP并且它可以工作,但我想使用.Net库.
我有一个程序,我写的是下载到文件.第二个文件不是必要的,只包括一些时间.如果未包含第二个文件,则会返回HTTP 404错误.
现在,问题是当返回此错误时,它将结束整个程序.我想要的是继续该程序并忽略HTTP错误.所以,我的问题是如何HTTP 404从WebClient.DownloadFile请求中捕获错误?
这是当前使用的代码::
WebClient downloader = new WebClient();
foreach (string[] i in textList)
{
String[] fileInfo = i;
string videoName = fileInfo[0];
string videoDesc = fileInfo[1];
string videoAddress = fileInfo[2];
string imgAddress = fileInfo[3];
string source = fileInfo[5];
string folder = folderBuilder(path, videoName);
string infoFile = folder + '\\' + removeFileType(retrieveFileName(videoAddress)) + @".txt";
string videoPath = folder + '\\' + retrieveFileName(videoAddress);
string imgPath = folder + '\\' + retrieveFileName(imgAddress);
System.IO.Directory.CreateDirectory(folder);
buildInfo(videoName, videoDesc, …Run Code Online (Sandbox Code Playgroud) 我想使用HTTP Web服务,我已经为wp7开发了一个应用程序.
我使用WebClient类,但我不能将它用于Windows 8("错误:无法找到类型或命名空间").
我还能用什么?
你能给我一个代码样本吗?
当命名空间不存在时,Microsoft是否有一个站点可以提供帮助?
我对Python不太熟悉.我试图从以下页面中提取艺术家名称(开始:)):http://www.infolanka.com/miyuru_gee/art/art.html.
如何检索页面?我的两个主要问题是; 使用什么功能以及如何从页面中过滤掉无用的链接?
我刚开始尝试使用C#WebClient.我所拥有的是下面的代码,它从网站获取html代码并将其写入.txt文件.我唯一的问题是有些网站要求您在使用网站之前接受cookie.这导致的不是将真实的网站html代码写入.txt文件,而是编写cookie popup html代码.
码:
string downloadedString;
System.Net.WebClient client;
client = new System.Net.WebClient();
//"http://nl.wikipedia.org/wiki/Lijst_van_spelers_van_het_Nederlands_voetbalelftal"
downloadedString = client.DownloadString(textBox1.Text);
using (StreamWriter write = new StreamWriter("Data.txt"))
{
write.Write(downloadedString);
}
Run Code Online (Sandbox Code Playgroud)
那么这个解决方案是什么?有人可以指引我走正确的道路吗?
我可以在IE中手动下载.
但是,使用以下代码
WebClient client = new WebClient();
client.DownloadFile(address, filename);
Run Code Online (Sandbox Code Playgroud)
显示例外:403禁止
怎么了?我怎样才能做到这一点?
其他
我想知道在使用时我应该保护自己的例外情况WebClient.DownloadString.
这是我目前正在使用它的方式,但我相信你们可以建议更好的更强大的异常处理.
例如,在我的头顶:
处理这些情况并将异常抛出到UI的首选方法是什么?
public IEnumerable<Game> FindUpcomingGamesByPlatform(string platform)
{
string html;
using (WebClient client = new WebClient())
{
try
{
html = client.DownloadString(GetPlatformUrl(platform));
}
catch (WebException e)
{
//How do I capture this from the UI to show the error in a message box?
throw e;
}
}
string relevantHtml = "<tr>" + GetHtmlFromThisYear(html);
string[] separator = new string[] { "<tr>" };
string[] individualGamesHtml = relevantHtml.Split(separator, StringSplitOptions.None);
return ParseGames(individualGamesHtml);
}
Run Code Online (Sandbox Code Playgroud) 我基本上都在寻找同样的问题: 当服务器返回错误时,使用WebClient访问响应体的方法是什么?
但到目前为止还没有提供任何答案.
服务器返回"400错误请求"状态,但有详细的错误说明作为响应正文.
有关使用.NET WebClient访问该数据的任何想法?它只是在服务器返回错误状态代码时抛出异常.
webclient ×10
c# ×8
.net ×2
html ×2
bad-request ×1
cookies ×1
downloadfile ×1
python ×1
vb.net ×1
webexception ×1
windows-8 ×1