您希望在System.Net.HttpWebRequest和System.Net.WebClient中看到哪些功能不是开箱即用的?
哪些课程不能或不能轻易实现?
我希望能够:
我有这个为ex: Link
这段代码:
const String nick = "Alex";
const String log = "http://demonscity.combats.com/zayavka.pl?logs=";
foreach (DateTime cd in dateRange)
{
string str = log + String.Format("{0:MM_dd_yy}", cd.Date) + "&filter=" + nick;
String htmlCode = wc.DownloadString(str);
}
Run Code Online (Sandbox Code Playgroud)
返回一些东西...."<\ b\0\0\0\0\0 \0я•XYsЫЦ~зЇёѕ™d)bг.тBҐ$ЪRЖ'<2УN&сh@р'"\ f\0J--_Фџђ§¤ нt|г6ќѕУЄђ0'IQtТґcμо№X(jі-Щ/Ђі|?g`yҐц"
其他链接工作正常.我认为问题在于代码页,我该如何解决?或者是服务器问题?
如果可以从使用标准化布局组织的网站读取信息,则意味着网站的控件/文本框/按钮等始终位于同一位置,但其所持有的数据/值会发生变化.
在C#WinForm应用程序中,我可以在后台打开该页面读取一些值并使用我的表单中的那些值吗?有没有办法在网页上引用特定的区域/项目,即使它只是标签效率低12倍?我完全在这里做梦吗?
同样,我不需要点击页面上的某些内容,只需阅读某个文本框中的内容或那种性质的内容.
如果我在Consoleapp中执行此代码,它可以正常工作:
string uriString = "http://url.com/api/v1.0/d/" + Username + "/some?amount=3&offset=0";
WebClient wc = new WebClient();
wc.Headers["Content-Type"] = "application/json";
wc.Headers["Authorization"] = AuthString.Replace("\\", "");
string responseArrayKvitteringer = wc.DownloadString(uriString);
Console.WriteLine(responseArrayKvitteringer);
Run Code Online (Sandbox Code Playgroud)
但是,如果我将代码移动到我的WP7项目,如下所示:
string uriString = "http://url.com/api/v1.0/d/" + Username + "/some?amount=3&offset=0";
WebClient wc = new WebClient();
wc.Headers["Content-Type"] = "application/json";
wc.Headers["Authorization"] = AuthString.Replace("\\", "");
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(uriString));
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
MessageBox.Show(e.Result);
}
Run Code Online (Sandbox Code Playgroud)
我得到了例外:使用此方法的请求不能有请求正文.
为什么?
解决方案是删除Content-type:
string uriString = "http://url.com/api/v1.0/d/" + Username + "/some?amount=3&offset=0";
WebClient wc = new WebClient();
//wc.Headers["Content-Type"] = "application/json"; …Run Code Online (Sandbox Code Playgroud) 我已经浪费了2天的时间才发现,WebBrowser控件中存在已知的内存泄漏(自2007年左右以来,他们还没有修复它)所以我决定在这里问一下,如何做我需要的东西.
直到现在,(使用WebBrowser ...),我一直在访问一个网站,(ctrl + a),将其粘贴到一个字符串,这就是全部.我的字符串中有网页的文本内容.完美地工作直到我发现它需要1 gb的内存一段时间后.是否可以通过HttpWebRequest,httpwebclient或其他任何方式做到这一点?
感谢回复,没有任何类似的线程(或者我没有发现任何,搜索并没有真正吸引我很多因为我现在很生气:P)
FORGOT TO ADD:我不想要HTML代码,我知道可以轻松搞定.就我而言,HTML代码是无用的.我确实需要文本用户在使用互联网浏览器打开页面时看到.
我想从远程服务器读取xml文件,但不知何故服务器没有响应我的请求.因此,Gzip抛出"GZip标头中的幻数不正确"异常.任何的想法?
private static string GetFile()
{
Uri uri = new Uri(@"http://www.iddaa.com.tr/XML/IDDAAMACPROGRAMI/index.htm?iddaadrawid=12.09.2012&iddaadrawide=13.09.2012&foraccess=KSsec654");
string xmlFile;
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create(uri);
req.UserAgent =
"MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers.Add("Accept-Encoding", "gzip,deflate");
using (GZipStream zip = new GZipStream(req.GetResponse().GetResponseStream(),
CompressionMode.Decompress))
{
var reader = new StreamReader(zip);
xmlFile = reader.ReadToEnd();
}
return xmlFile;
}
Run Code Online (Sandbox Code Playgroud) 当我尝试单击一个单独的asp页面WebClient用于从邮政编码下载完整的地址详细信息时,我一直收到错误消息。
The underlying connection was closed: An unexpected error occurred on a send.
Run Code Online (Sandbox Code Playgroud)
这仅在服务器上时发生。不仅如此,当我将URL粘贴到浏览器中时,它可以完美运行。这可能是防火墙设置吗?
这是我正在使用的代码(摘自此处的另一篇文章)
using (CookieAwareWebClient WC = new CookieAwareWebClient())
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;
string data = WC.DownloadString("https://www.myUrl.com/postcode.asp?postcode=" + postcode + "&houseNumber=" + Server.HtmlEncode(txtHouseNumber.Text));
}
Run Code Online (Sandbox Code Playgroud)
最初我只是使用它,结果相同:
WebClient client = new WebClient();
string address = "https://www.myUrl.com/postcode.asp?postcode=" + postcode + "&houseNumber=" + Server.HtmlEncode(txtHouseNumber.Text);
string data = client.DownloadString(address);
Run Code Online (Sandbox Code Playgroud)
该应用程序使用.NET 4 / C#构建,并通过iis6托管在Windows Server 2003上。
如果这是防火墙或安全设置,那会是什么?如果没有,对原因或解决方法有什么想法?非常感谢
更新-########################
好的,我也尝试使用HttpWebRequest,在第二行出现错误:
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(new Uri("https://www.myUrl.com/postcode.asp?postcode=AZ11ZA&houseNumber=1"));
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Run Code Online (Sandbox Code Playgroud)
该错误消息包含:
System.Threading.Thread.AbortInternal() …Run Code Online (Sandbox Code Playgroud) 鉴于:
public async Task<string> SendRequest(this string url)
{
var wc = new WebClient();
wc.DownloadDataCompleted += (s, e) =>
{
var buffer = e.Result;
using (var sr = new StreamReader(new MemoryStream(buffer)))
{
var result = await sr.ReadToEndAsync();
};
};
wc.DownloadDataAsync(new Uri(url));
}
}
Run Code Online (Sandbox Code Playgroud)
该声明:
var result = await sr.ReadToEndAsync();
Run Code Online (Sandbox Code Playgroud)
在设计器中显示错误,如下所示:"await运算符只能在异步lambda表达式中使用"

我不明白为什么会发生这样的消息,当我查看所有ReadToEndAsync示例时,它们看起来与此代码完全相同.
请指教...
我正在尝试从我的 Web 服务器下载文件夹,并且我想在进度条上显示下载了多少数据/总共下载了多少数据的进度。首先,我尝试使用WebClient.DownloadFile. 哪个工作完美,但没有触发DownloadProgressChangedEventHandler。我猜它只能通过异步下载激活。所以我已经将我的方法改写为WebClient.DownloadFileAsync. 这就是它变得复杂的地方。
例如,我的 Web 服务器上有 30 个文件,大小为 53 MB。我想下载所有 30 个文件并在进度条上显示下载进度(并在它下面显示带有 xx/53 MB 下载的标签)。
//Inicialized by opening dialog
private void DownloadForm_Shown(object sender, EventArgs e) {
WebClient client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileCompleted += client_DownloadFileCompleted;
startDownload();
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) {
progressBar.Value = e.ProgressPercentage;
labelProgress.Text = String.Format("Downloaded {0} of {1} bytes", e.BytesReceived, e.TotalBytesToReceive);
}
private void startDownload() {
//files contains all URL links
foreach (string str in files) …Run Code Online (Sandbox Code Playgroud) 我正在实现一个方法,以便相互下载多个文件.
我希望Method是异步的,所以我不会阻止UI.
这是下载单个文件的方法,并将Download-Task返回到上级方法,后者下载所有文件(进一步向下).
public Task DownloadFromRepo(String fileName)
{
// Aktuellen DateiNamen anzeigen, fileName publishing for Binding
CurrentFile = fileName;
// Einen vollqualifizierten Pfad erstellen, gets the path to the file in AppData/TestSoftware/
String curFilePath = FileSystem.GetAppDataFilePath(fileName);
// Wenn die Datei auf dem Rechner liegt, wird sie vorher gelöscht / Deletes the file on the hdd
FileSystem.CleanFile(fileName);
using (WebClient FileClient = new WebClient())
{
FileClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler((s, e) =>
{
Progress++;
});
// Wenn der Download abgeschlossen ist.
FileClient.DownloadFileCompleted += …Run Code Online (Sandbox Code Playgroud) webclient ×10
c# ×8
asynchronous ×3
.net ×2
async-await ×2
asp.net ×1
encoding ×1
gzip ×1
httprequest ×1
lambda ×1
task ×1
web-crawler ×1
winforms ×1
xml-parsing ×1