我正在尝试从 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) 禁止。
我现在用谷歌搜索了将近一个小时,但找不到任何可以帮助我的东西。我是一名初级程序员,从 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 类进行网络抓取,所以我想这样做。
寻找一种更普遍接受的等待模式WebClient:
限制条件:
Task.Run(async () => await method())Download调用该方法时,它只需要像普通方法一样返回字符串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) 下面是我在 powershell 中获取网页中的链接的代码。我间歇性地收到“无法索引到空数组”异常。这段代码有什么问题吗?需要帮助。
$Download = $wc.DownloadString($Link)
$List = $Download -split "<a\s+" | %{ [void]($_ -match "^href=[`'`"]([^`'`">\s]*)"); $matches[1] }
Run Code Online (Sandbox Code Playgroud) 我收到了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) 您好我已经创建了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"来测试这些服务.请指导我
提前致谢 !!!!
我是一个arduino新手,我想为我的论文建立一个网络项目.我使用arduino uno作为微控制器,DHT11用于温度传感器,Enc28j60用于以太网屏蔽.该项目用于控制LED并通过网站监控温度和湿度.我想将arduino设置为客户端.我想在局域网模式下模拟这个项目.我想问一下:1.如何将arduino设置为客户端?2.如何从arduino接收温度数据并将其存储到数据库(mysql)?3.如何通过网站控制led?
我曾尝试将arduino作为服务器,并且它成功了.但我混淆了如何设置为客户端谢谢你,
有人可以帮帮我吗?
我需要在图像中找到字母.我需要一个帮助来编写一个算法来解码图像.
我把图像转换成了一个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
我有一个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) 我想从字符串中获取单个数据,任何人都可以帮助我单独分割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)