标签: webclient

Download Files using C# from https Website

我正在尝试从 https 网站下载 pdf 文件,但它不起作用。我是 C# 新手,所以做了一些研究并找到了一个简单的代码。更糟糕的是,我得到的异常非常普遍。请帮助。

    static void Main(string[] args)
    {

        string file_ = "https://www.nseindia.com/content/circulars/CMPT34469.pdf";
        string path_ = @"E:\RSR\Office\EEP\FileDownloader\output\Hello_World.pdf";

        WebClient wc_ = new WebClient();
        wc_.DownloadFile(file_, path_);
    }
Run Code Online (Sandbox Code Playgroud)

异常:System.dll 中发生类型为“System.Net.WebException”的未处理异常附加信息:远程服务器返回错误:(403) 禁止。

c# https webclient

0
推荐指数
1
解决办法
7811
查看次数

如何使用 C# WebClient 向 Google Translate Api 发送 POST 请求?

我现在用谷歌搜索了将近一个小时,但找不到任何可以帮助我的东西。我是一名初级程序员,从 TeamTreeHouse 学习了 C# 序列化课程。在这里我也学会了如何使用WebClient。

现在我认为一个好的实践项目是制作一个翻译应用程序,将用户在 POST 请求中输入的内容发送给谷歌,然后谷歌将答案作为我反序列化的 Json 返回。

问题是我通读了 api 的文档,但我很困惑我应该向 google 发送什么内容以及到底如何做到这一点?

我知道这个方法webclient.Headers.Add(arguments here),但我真的不知道它还需要什么。

您可以在这里找到一个示例:

https://cloud.google.com/translate/docs/translated-text

https://translation.googleapis.com/language/translate/v2 每个翻译请求需要三个查询参数:

目标语言:使用目标参数指定要翻译成的语言。源文本字符串:使用 q 参数指定要翻译的每个文本字符串。API key:使用 key 参数来识别您的应用程序。如果您使用 OAuth 2.0 服务帐户凭据(推荐),请不要提供此参数。

所以问题是该示例 URL 中没有占位符,我可以将 api 密钥 + 源文本字符串放入目标语言。

那么我到底应该向 google 发送什么,以便它知道我想要什么并返回 JSON 文件?

也许有人可以帮助我。我知道有一个官方库专门用于此目的,但我想练习序列化并使用 WebClient 类进行网络抓取,所以我想这样做。

c# webclient google-translate

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

等待下载完成时取消 WebClient 下载

寻找一种更普遍接受的等待模式WebClient

  • 下载文件(可能需要几百毫秒或几分钟)
  • 等待下载完成后再执行任何其他工作
  • 定期检查另一个类的标志 (bool) 并根据需要取消下载(无法修改此类)

限制条件:

  • 不能使用 async/await 除非它是类似的Task.Run(async () => await method())
  • Download调用该方法时,它只需要像普通方法一样返回字符串
  • 可以使用 .Net 4.5 和 Roslyn 编译器的任何功能
  • 是否使用WebClient.DownloadFileTaskAsync或 都没有区别;DownloadFileAsync只需能够根据需要使用取消下载WebClient

当前的实现似乎有效,但似乎不太正确。是否有比使用while循环并在使用时Thread.Sleep定期检查更普遍可接受的替代方案?otherObject.ShouldCancelWebClient

private string Download(string url)
{
    // setup work
    string fileName = GenerateFileName();

    // download file
    using (var wc = new WebClient()) 
    {
        wc.DownloadFileCompleted += OnDownloadCompleted

        Task task = wc.DownloadFileTaskAsync(url, fileName);

        // Need to wait until either the download is completed …
Run Code Online (Sandbox Code Playgroud)

c# webclient cancellation

0
推荐指数
1
解决办法
2788
查看次数

使用正则表达式从 Powershell 中的网页获取链接

下面是我在 powershell 中获取网页中的链接的代码。我间歇性地收到“无法索引到空数组”异常。这段代码有什么问题吗?需要帮助。

$Download = $wc.DownloadString($Link) 
$List = $Download -split "<a\s+" | %{ [void]($_ -match "^href=[`'`"]([^`'`">\s]*)"); $matches[1] }
Run Code Online (Sandbox Code Playgroud)

regex powershell webclient

0
推荐指数
1
解决办法
4258
查看次数

C++错误C3867

我收到了followint错误:

错误C3867:'std :: basic_string <_Elem,_Traits,_Ax> :: c_str':函数调用缺少参数列表; 使用'&std :: basic_string <_Elem,_Traits,_Ax> :: c_str'创建指向成员的指针

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#using <System.dll>
#include <sstream>
#include <string>
using namespace System::Net;
using namespace std;

int main(array<System::String ^> ^args)
{
int input;
cout << "Welcome to the prize draw!!" << endl;
cout << "Please enter your age - ";
cin >> input;
cin.ignore(); 
if (input < 13){
    cout << "You must be 13 or over to enter" << endl;
}
else
{
    cout << …
Run Code Online (Sandbox Code Playgroud)

c++ string webclient system

-1
推荐指数
1
解决办法
3403
查看次数

如何使用HTTPWebRequest或Webclient调用Rest WCF服务?

您好我已经创建了WCF服务,服务合同和数据合同粘贴在下面

[ServiceContract]
public interface IRestWithXML
{
    [OperationContract]
    [WebInvoke(Method = "Post", UriTemplate = "DoWork", RequestFormat=                WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string DoWork(Test objtest);

    [OperationContract]
    [WebInvoke(Method = "Post", UriTemplate = "Method?test={strtest}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string Method(Test objtest,string strtest);

}          

[DataContract]
public class Test
{
    [DataMember]
    public string id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我应该如何在.NET中测试这些服务.我可以通过将方法"Post"更改为"GET"来测试方法.

但我必须使用"Post"来测试这些服务.请指导我

提前致谢 !!!!

c# rest wcf webclient httpwebrequest

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

Arduino UNO + Enc28j60 + DHT11 + PHP和Mysql(局域网模式)

我是一个arduino新手,我想为我的论文建立一个网络项目.我使用arduino uno作为微控制器,DHT11用于温度传感器,Enc28j60用于以太网屏蔽.该项目用于控制LED并通过网站监控温度和湿度.我想将arduino设置为客户端.我想在局域网模式下模拟这个项目.我想问一下:1.如何将arduino设置为客户端?2.如何从arduino接收温度数据并将其存储到数据库(mysql)?3.如何通过网站控制led?

我曾尝试将arduino作为服务器,并且它成功了.但我混淆了如何设置为客户端谢谢你,

有人可以帮帮我吗?

webclient arduino

-2
推荐指数
1
解决办法
2657
查看次数

如何将带有字母的图像转换为文本?

我需要在图像中找到字母.我需要一个帮助来编写一个算法来解码图像.

我把图像转换成了一个ByteArrayOutputStream但我不知道我用它做了什么.

有我的java代码:

URL url = new URL(urlImg);
WebClient webClient = new WebClient(BrowserVersion.getDefault());
WebRequest reqImg = new WebRequest(url);
reqImg.setHttpMethod(HttpMethod.GET);
InputStream imgStream = webClient.getPage(reqImg).getWebResponse().getContentAsStream();
BufferedImage img = ImageIO.read(imgStream);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img, "png", out);
Run Code Online (Sandbox Code Playgroud)

依赖关系:net.sourceforge.htmlunit htmlunit 2.15

JDK 1.6.0_43

java captcha bufferedimage webclient javax.imageio

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

使用DownloadDataAsync下载AWAIT多个文件

我有一个zip文件创建者,它接收一个String[]Urls,并返回一个包含所有文件的zip文件String[]

我想会有很多这方面的例子,但我似乎找不到"如何异步下载多个文件并在完成后返回"的答案

如何一次下载{n}个文件,仅在所有下载完成后返回词典?

private static Dictionary<string, byte[]> ReturnedFileData(IEnumerable<string> urlList)
{
    var returnList = new Dictionary<string, byte[]>();
    using (var client = new WebClient())
    {
        foreach (var url in urlList)
        {
            client.DownloadDataCompleted += (sender1, e1) => returnList.Add(GetFileNameFromUrlString(url), e1.Result);
            client.DownloadDataAsync(new Uri(url));
        }
    }
    return returnList;
}

private static string GetFileNameFromUrlString(string url)
{
    var uri = new Uri(url);
    return System.IO.Path.GetFileName(uri.LocalPath);
}
Run Code Online (Sandbox Code Playgroud)

.net c# webclient async-await webclient-download

-3
推荐指数
1
解决办法
1845
查看次数

如何从字符串中拆分单个数据..?

我想从字符串中获取单个数据,任何人都可以帮助我单独分割ID内容.我的代码存储结果值:

private void sendPostCompleted(object sender, UploadStringCompletedEventArgs e)
{
    try
    {
        MessageBox.Show("Success..");
        MessageBox.Show(e.Result);
        string res=(string)e.Result;//here res contains my data..
        //need to get ID value alone.
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# split webclient windows-phone-7

-5
推荐指数
1
解决办法
248
查看次数