标签: webclient

如何在发布数据后阅读WebClient响应?

看看代码:

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输出?

.net c# webclient

18
推荐指数
2
解决办法
4万
查看次数

正确处理两个WebException

我正在努力处理两种不同WebException的问题.

基本上他们在打电话后处理 WebClient.DownloadFile(string address, string fileName)

AFAIK,到目前为止我必须处理两个,两个WebException:

  • 无法解析远程名称(即没有网络连接访问服务器下载文件)
  • (404)文件不正确(即服务器上不存在该文件)

可能会有更多,但这是我迄今为止发现最重要的内容.

那么我应该如何处理这个问题,因为它们都是,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.我会假设有更好或更精确的方法来做到这一点?

谢谢

c# webclient exception-handling downloadfile webexception

18
推荐指数
1
解决办法
2万
查看次数

如何让WebClient使用Cookies?

我希望VB.net WebClient能够记住cookie.

我已经搜索并尝试了许多重载类.

我想通过POST登录到一个网站,然后POST到另一个页面并获取其内容,同时仍保留我的会话.

这是否可以使用VB.net而不使用WebBrowser控件?

我尝试过Chilkat.HTTP并且它可以工作,但我想使用.Net库.

vb.net webclient

18
推荐指数
2
解决办法
3万
查看次数

如何检查WebClient请求是否有404错误

我有一个程序,我写的是下载到文件.第二个文件不是必要的,只包括一些时间.如果未包含第二个文件,则会返回HTTP 404错误.

现在,问题是当返回此错误时,它将结束整个程序.我想要的是继续该程序并忽略HTTP错误.所以,我的问题是如何HTTP 404WebClient.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)

c# webclient

18
推荐指数
4
解决办法
4万
查看次数

Windows 8中不存在WebClient类

我想使用HTTP Web服务,我已经为wp7开发了一个应用程序.

我使用WebClient类,但我不能将它用于Windows 8("错误:无法找到类型或命名空间").

我还能用什么?

你能给我一个代码样本吗?

当命名空间不存在时,Microsoft是否有一个站点可以提供帮助?

c# webclient windows-8 windows-runtime visual-studio-2012

18
推荐指数
1
解决办法
2万
查看次数

如何使用Python获取HTML文件?

我对Python不太熟悉.我试图从以下页面中提取艺术家名称(开始:)):http://www.infolanka.com/miyuru_gee/art/art.html.

如何检索页面?我的两个主要问题是; 使用什么功能以及如何从页面中过滤掉无用的链接?

html python webclient

17
推荐指数
4
解决办法
7万
查看次数

接受WebClient中的Cookie?

我刚开始尝试使用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)

那么这个解决方案是什么?有人可以指引我走正确的道路吗?

html c# cookies webclient

16
推荐指数
3
解决办法
3万
查看次数

15
推荐指数
4
解决办法
3万
查看次数

处理WebClient.DownloadString的正确方法的异常

我想知道在使用时我应该保护自己的例外情况WebClient.DownloadString.

这是我目前正在使用它的方式,但我相信你们可以建议更好的更强大的异常处理.

例如,在我的头顶:

  • 没有网络连接.
  • 服务器返回404.
  • 服务器超时.

处理这些情况并将异常抛出到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)

.net c# webclient

15
推荐指数
3
解决办法
4万
查看次数

WebClient - 获取错误状态代码的响应正文

我基本上都在寻找同样的问题: 当服务器返回错误时,使用WebClient访问响应体的方法是什么?

但到目前为止还没有提供任何答案.

服务器返回"400错误请求"状态,但有详细的错误说明作为响应正文.

有关使用.NET WebClient访问该数据的任何想法?它只是在服务器返回错误状态代码时抛出异常.

c# webclient bad-request

15
推荐指数
2
解决办法
2万
查看次数