为了让System.Net使用Microsoft Visual C#2008 Express Edition,我需要做些什么吗?我似乎无法获得任何Web类型控件或类工作..下面的WebClient示例总是抛出异常" 无法连接到远程服务器 "..因此我无法加载WebBrowser控件一页也是.
这是代码(已编辑):
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
using (WebClient client = new WebClient()) {
string s = client.DownloadString("http://www.google.com");
this.textBox1.Text = s;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的形式,只有一个文本框控件(多行设置为true).异常被抛出DownloadString(...)线上.我也尝试过使用WebRequest..同样的例外!
编辑:
我连接到Linksys WRT54G路由器,它直接连接到我的电缆调制解调器.我不是代理服务器的后面,虽然我确实运行proxycfg -u了,但我得到了:
Updated proxy settings
Current WinHTTP proxy settings under:
HKEY_LOCAL_MACHINE\
SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections\
WinHttpSettings :
Direct access (no proxy server).
Run Code Online (Sandbox Code Playgroud)
我使用的是Windows …
在Silverlight应用程序中,我有时需要连接到托管应用程序的网站.为了避免在我的Silverlight应用程序中对网站进行硬编码,我使用如下代码:
WebClient webClient = new WebClient();
Uri baseUri = new Uri(webClient.BaseAddress);
UriBuilder uriBuilder = new UriBuilder(baseUri.Scheme, baseUri.Host, baseUri.Port);
// Continue building the URL ...
Run Code Online (Sandbox Code Playgroud)
WebClient为了访问XAP文件的URL,创建实例感觉非常笨拙.还有其他选择吗?
在我的一个应用程序中,我使用WebClient类从Web服务器下载文件.有时,应用程序会根据Web服务器下载数百万个文档.似乎有很多文档时,WebClient不能很好地扩展性能.
此外,似乎WebClient即使在成功下载特定文档后也不会立即关闭它为WebServer打开的连接.
我想知道我还有其他选择.
更新:我还注意到,每次下载WebClient都会执行身份验证握手.由于我的应用程序只与单个Web服务器进行通信,因此我期待看到此握手一次.WebClient的后续调用是否应该重用身份验证会话?
更新:我的应用程序还调用了一些Web服务方法,对于这些Web服务调用,似乎重用了身份验证会话.此外,我正在使用WCF与Web服务进行通信.
我可能在这里遗漏了一些东西,但是当我使用WebClient并查找DownloadString方法时(如书中的示例所示),我只看到了DownloadStringAsync.
我错过了什么导入?
我正在使用WebClient.DownloadString()方法下载一些数据.我使用以下代码:
static void Main(string[] args)
{
string query = "select+%3farticle+%3fmesh+where+{+%3farticle+a+npg%3aArticle+.+%3farticle+npg%3ahasRecord+[+dc%3asubject+%3fmesh+]+.+filter+regex%28%3fmesh%2c+\"blood\"%2c+\"i\"%29+}";
NameValueCollection queries = new NameValueCollection();
queries.Add("query", query);
//queries.Add("output", "sparql_json");
using (WebClient wc = new WebClient())
{
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
wc.QueryString = queries;
string result = wc.DownloadString("http://data.nature.com/sparql");
Console.WriteLine(result);
}
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,tt工作正常,并给我一个xml字符串作为输出.但我想得到一个JSON输出,因此我没有评论该行
queries.Add("output", "sparql_json");
Run Code Online (Sandbox Code Playgroud)
并执行相同的程序,似乎从服务器获取错误消息.
但是,如果我尝试使用Web浏览器并使用相同的URL(如下所示),它会按预期提供JSON: 在浏览器中有效的URL
我想知道问题是什么.特别是当它在浏览器中工作而不使用webclient时.webclient在这里做了哪些不同的事情?
请注意,我也尝试将查询指定为
query + "&output=sparql_json"
但这也不起作用.
有人可以告诉我问题可能是什么?
谢谢
它是一个类库,它被称为网页
try
{
System.Net.WebClient wc = WebAccess.GetWebClient();
wc.UploadStringCompleted += new System.Net.UploadStringCompletedEventHandler(wc_UploadStringCompleted);
wc.UploadStringAsync(new Uri("http://" + MobileWLCUrl + urlCreateCacheAPI), GetJSONforGetMenuDetails(Id, MenuIds));
}
catch (Exception ex) { EngineException.HandleException(ex); }
void wc_UploadStringCompleted(object sender, System.Net.UploadStringCompletedEventArgs e)
{
string result = e.Result;
EngineException.CreateLog("Cache Created (for Menus: " + MenuIds + ") in API for LocationId: " + LocId);
}
Run Code Online (Sandbox Code Playgroud)
嗨伙计们,我试图异步地打这个网址而且它给了我这个错误,任何帮助都会受到关注.
Asynchronous operations are not allowed in this context.
Page starting an asynchronous operation has to have the Async attribute set to true
and an asynchronous operation …Run Code Online (Sandbox Code Playgroud) 使用WebClient类时,以编程方式跟踪完整原始HTTP请求的最佳方法是什么?
我不知道如何在Windows Server 2012上安装Web客户端服务.任何帮助将不胜感激.
谢谢
我想知道是否可以将cookie从一个Web客户端复制到另一个Web客户端.
我正在使用并行Web请求,它在每个新线程上创建Web客户端的新实例.
信息是敏感的,需要使用发布请求进行授权并保存cookie.所以基本上那些新的Web客户端实例无法访问.我不想授权每个正在创建的Web客户端,所以我想知道是否可以以某种方式将cookie从一个Web客户端复制到另一个Web客户端.
public class Scrapper
{
CookieAwareWebClient _web = new CookieAwareWebClient();
public Scrapper(string username, string password)
{
this.Authorize(username, password); // This sends correct post request and then it sets the cookie on _web
}
public string DowloadSomeData(int pages)
{
string someInformation = string.Empty;
Parallel.For(0, pages, i =>
{
// Cookie is set on "_web", need to copy it to "web"
var web = new CookieAwareWebClient(); // No authorization cookie here
html = web.DownloadString("http://example.com/"); // Can't access …Run Code Online (Sandbox Code Playgroud) 我试图在字符串中存储来自以下url的JSON响应.大多数情况下,我运行代码,JSON响应的最后几个字符被切断.当您在浏览器中访问该URL时,将显示完整的JSON响应.有时我的代码有效,但大多数情况下它返回部分内容.
如果我减少了我请求的网址中查询的长度,我会收到更多内容,更接近完整响应,这很奇怪.如果我完全删除查询字符串,通常会返回完整的JSON响应.问题是我想保持查询字符串不变.
我尝试了各种替代方法,包括更改编码,使用HttpWebRequest/HttpWebResponse,将响应复制到MemoryStream,使用字节缓冲区读取响应,更改协议版本等.
关于如何获得完整响应并能解释发生了什么的任何想法?谢谢!
System.Net.WebClient wc = new System.Net.WebClient();
string data = wc.DownloadString("http://static.arcgis.com/attribution/World_Topo_Map?f=json&callback=dojo.io.script.jsonp_dojoIoScript19._jsonpCallback");
Console.Write(data);// String should end with ",-119.2]}]}]});"
Console.Read();
Run Code Online (Sandbox Code Playgroud) webclient ×10
c# ×7
.net ×5
asynchronous ×1
download ×1
exception ×1
http ×1
parallels ×1
silverlight ×1
system.net ×1
url ×1