目前我有一个应用程序从我的Web应用程序接收上传的文件.我现在需要将该文件传输到恰好位于同一网络上的文件服务器(但事实并非总是如此).
我试图在C#.NET中使用webclient类.
string filePath = "C:\\test\\564.flv";
try
{
WebClient client = new WebClient();
NetworkCredential nc = new NetworkCredential(uName, password);
Uri addy = new Uri("\\\\192.168.1.28\\Files\\test.flv");
client.Credentials = nc;
byte[] arrReturn = client.UploadFile(addy, filePath);
Console.WriteLine(arrReturn.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
位于192.168.1.28的计算机是文件服务器,共享c:\ Files.截至目前,我收到登录失败的错误用户名或密码错误,但我可以打开资源管理器并输入该路径成功登录.我也可以使用远程桌面登录,所以我知道用户帐户有效.
有关此错误的任何想法?是否可以像这样直接传输文件?使用webclient类或者其他类?
我有一个客户端,它可以生成有限数量的并发Web请求.我为此目的使用WebClient.我目前有一个WebClient-s池,我创建一次并使用任何一个空闲的.
这种方法虽然变得有点麻烦,但我想知道预先构建的WebClient实例集合是否有任何好处,或者是否在运行中创建它们不会太麻烦?
在调试资源泄漏问题时,我注意到System.Net.WebException(非一次性类型)包含对System.Net.WebResponse(一次性类型)的引用.我想知道在显式处理WebResponse以下代码段中的as 时是否应该处理此引用.
using (WebClient client = new WebClient())
{
WebException ex = Assert.Throws<WebException>(() => client.OpenRead(myUri));
Assert.That(
((HttpWebResponse)ex.Response).StatusCode,
Is.EqualTo(HttpStatusCode.ServiceUnavailable));
}
Run Code Online (Sandbox Code Playgroud)
该WebException.WebResponse引用是现有引用的副本WebClient.我认为它会被处理掉,WebClient.Dispose但事实并非如此,因为WebClient它不会覆盖受保护的Component.Dispose(bool)基本方法.实际上,反汇编表明WebResponse资源从未被处理掉,而是在不再需要时设置为null.
public Stream OpenRead(Uri address)
{
Stream stream2;
// --- removed for brevity ---
WebRequest request = null;
this.ClearWebClientState();
try
{
request = this.m_WebRequest = this.GetWebRequest(this.GetUri(address));
Stream responseStream = (this.m_WebResponse = this.GetWebResponse(request)).GetResponseStream();
// --- removed for brevity ---
stream2 = responseStream; …Run Code Online (Sandbox Code Playgroud) 我有Webclient的问题.
这很慢.从一个网站下载串串大约需要3-5秒.我没有任何网络问题.
这是我的Modifed WebClient.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace StatusChecker
{
class WebClientEx: WebClient
{
public CookieContainer CookieContainer { get; private set; }
public WebClientEx()
{
CookieContainer = new CookieContainer();
ServicePointManager.Expect100Continue = false;
Encoding = System.Text.Encoding.UTF8;
WebRequest.DefaultWebProxy = null;
Proxy = null;
}
public void ClearCookies()
{
CookieContainer = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = CookieContainer;
}
return …Run Code Online (Sandbox Code Playgroud) 我找不到任何指定WebClient引发其事件的线程的文档.我运行了一些测试并确定了以下内容:
如果从UI线程调用(例如从事件处理程序调用),则将在该线程上执行事件处理程序.作为测试,我在调用OpenReadAsync之后添加了一个无限循环.从未调用过事件处理程序.
如果没有UI线程,就像在控制台应用程序中那样,事件处理程序将在线程池线程上执行.在这种情况下,如果我想在应用程序的其余部分提供一些结果,我将不得不注意线程问题.
这种行为记录在哪里吗?我一无所获.
我有关于C#的新异步功能的基本相同的问题 - 最终,必须执行异步代码.当没有UI线程时,这还会产生一个线程池线程吗?那反过来又需要线程安全代码吗?
我觉得我在这里遗漏了一些东西 - 我只能找到很少有关于此的信息,但这对我来说似乎很重要.
我需要一些奇怪的WebClient.UploadFileAsync()行为的帮助.我正在将文件上传到远程HTTP服务器(nginx)usind POST方法.POST通过PHP脚本(地址引用)处理.
我有这个简单的代码
public void uploadFile(string filePath)
{
webClient = new WebClient();
webClient.Credentials = new NetworkCredential(Constant.HTTPUsername,Constant.HTTPPassword);
webClient.Headers.Add("Test", TestKey);
webClient.UploadProgressChanged += webClient_UploadProgressChanged;
webClient.UploadFileCompleted += webClient_UploadFileCompleted;
try
{
webClient.UploadFileAsync(new Uri(Address), "POST", filePath);
}
catch (Exception error)
{
throw new CustomException(error.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
在UploadProgressChanged中,我只需使用给定的ProgressPercentage更新progressBar.第一个问题是报告的进度百分比,任何文件大小为:
[17.38.14] Progress: 0 Bytes Sent: 175 / 269264
[17.38.14] Progress: 1 Bytes Sent: 8367 / 269264
[17.38.14] Progress: 3 Bytes Sent: 16559 / 269264
[17.38.14] Progress: 4 Bytes Sent: 24751 / 269264
[17.38.14] Progress: 6 Bytes Sent: 32943 / …Run Code Online (Sandbox Code Playgroud) 我使用此代码登录:
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("example.com");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl = "example.com";
string postData = String.Format("my parameters");
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies);
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close(); …Run Code Online (Sandbox Code Playgroud) 我有一段这样的代码:
public class NoFollowWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.AllowAutoRedirect = false;
return request;
}
}
Run Code Online (Sandbox Code Playgroud)
每当我将它添加到.cs文件中时,Visual Studio 2012就是无限的智慧,将我的C#源文件转换为"设计时组件".因此,当我现在双击该文件时,我没有看到我的C#代码,而是看到"要向您的类中添加组件,请从工具箱中拖动它们并使用"属性"窗口设置其属性".
我知道我可以右击并执行"查看代码",但这非常烦人.
反正有没有迫使Visual Studio不假设我正在制作一个组件,或者我关心他们的愚蠢的视觉设计师,这对我的班级没有用处?
在.NET 4.5中使用WebClient,HttpWebRequest和HttpClient类可以发送多少并发/并行请求是否有限制?或者,如果对Web API或服务器没有限制,您是否可以发送无限制的并行请求?对不起,如果问题太广泛,请纠正我,以便缩小范围.
我在 Spring Boot Web 客户端发送请求正文时遇到了一些问题。尝试发送如下正文:
val body = "{\n" +
"\"email\":\"test@mail.com\",\n" +
"\"id\":1\n" +
"}"
val response = webClient.post()
.uri( "test_uri" )
.accept(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(body))
.exchange()
.block()
Run Code Online (Sandbox Code Playgroud)
它不工作。请求正文应为 JSON 格式。 请让我知道我哪里做错了。
webclient ×10
c# ×7
.net ×3
asynchronous ×1
components ×1
cookies ×1
dispose ×1
file-upload ×1
fileserver ×1
http ×1
httpclient ×1
kotlin ×1
networking ×1
spring-boot ×1
system.net ×1