我有一些客户端代码将Outlook电子邮件上传到文档库,只要路径指向文档库的根,它就可以正常工作.
@"https://<server>/sites/<subweb>/<customer>/<teamweb>/<Documents>/" + docname;
Run Code Online (Sandbox Code Playgroud)
是这个函数中的projectUrl:
public bool SaveMail(string filepath, string projectUrl)
{
try
{
using (WebClient webclient = new WebClient())
{
webclient.UseDefaultCredentials = true;
webclient.UploadFile(projectUrl, "PUT", filepath);
}
}
catch(Exception ex)
{
//TO DO Write the exception to the log file
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
但是我无法弄清楚如何上传到现有文件夹,即同一文档库中的"电子邮件".甚至谷歌似乎都不知道答案:-)
注意:我知道我可以使用SharePoint中的复制Web服务将文件移动到其最终目的地,但这更像是一种解决方法.
我什么时候才能学会不工作到深夜:-(
抱歉,这个问题.Igalse是对的,我只需要在URL中添加"emailils /".我可以发誓我已经尝试过了,但是再一次看起来我确实没有.
我有一个字符串,我在服务器上Gzip并使用WebClient类下载到客户端.当我尝试解压缩它时,我收到错误信息,表明Magic Number丢失了.我已经尝试过GZipStream类和解决这个问题的ICSharpLib方法,所以我很茫然.
如果我省略通过WebClient下载的步骤(使用将数据作为byte []返回的DownloadData),压缩/解压缩工作,所以我只能假设数据被截断或损坏有些问题,但是因为它是压缩数据,我不知道如何调试它.
这是代码片段,似乎是有问题的部分:
byte[] response
try {
response = client.DownloadData(Constants.GetSetting("SyncServer"));
} catch {
MessageBox.Show("There was a problem synchronizing the data. Please try verify the supplied credentials or try again later.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int rows = SQLiteAPI.ImportStatHistoryXML(CurrentUser.User, myCampus, Convert.ToBase64String(response));
public static int ImportStatHistoryXML(Person tempPerson, Campus tempCampus, string xmlFile) {
byte[] encryptedFile = Convert.FromBase64String(xmlFile);
MemoryStream memStream = new MemoryStream(encryptedFile);
memStream.ReadByte();
GZipInputStream stream = new GZipInputStream(memStream);
MemoryStream memory = new MemoryStream();
byte[] writeData = new byte[4096];
int size; …Run Code Online (Sandbox Code Playgroud) 我正在使用它来下载文件并获取%信息和完成的信息.我想知道如何获取正在下载的文件的大小以及URL远程地址和保存文件的本地地址.
private void Form1_Load_1(object sender, EventArgs e)
{
label21.Text = "Download in progress...";
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://www.somesite.com/Update/Updates.zip.010"), @"Updates.zip.010");
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage; //Progress Bar Handler
label1.Visible = true;
label1.Text = progressBar1.Value.ToString() + " %"; //Adds percent to a label
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
label11.Visible = true;
label11.Text = "Done";
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
public partial class WebClient : PhoneApplicationPage
{
public WebClient()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WebClient webclient = new WebClient();
Run Code Online (Sandbox Code Playgroud)
现在,当我说webClient.,我希望在IntelliSense下拉列表中看到DownloadStringCompleted,但我没看到.当我强制使用它时,当然它不会编译.问题是什么?
我正在测试WebClient以查看它是否在我的项目中使用,因为我厌倦了异步调用和与HttpWebRequest相关联的多个线程
我编写了一个非常简单的控制台应用程序,它只是使用以下代码下载文件:
System.Net.WebClient web = new System.Net.WebClient ();
web.DownloadFile ("http://www.google.com", "file.txt");
Run Code Online (Sandbox Code Playgroud)
在我以前的开发机器上,这非常快.一旦我执行了该DownloadFile方法,请求就越过了线路,响应很快就会响起.我使用ProcMon工具检查了这种行为.
然而,在切换到另一台机器之后,我发现它比我的初始开发盒要强大得多,我注意到在调用时大约5-8秒不会发生任何事情DownloadFile.使用浏览器访问相同的URL会显示几乎立即的结果.
在花了几个小时研究这个,进入.NET源代码之后,我终于放弃了System.Net.ServicePointManager.FindServicePoint踩到某种程度上不再对我有意义的地方,以及一些代理解析似乎有原因的地方.
我终于关闭了Internet Explorer 8(我运行的是Windows 7 x64)中的" Internet选项"对话框的" 局域网设置"对话框中的" 自动检测设置",而对话框中没有设置复选框.这神奇地使一切都做得非常非常快.没有更多的延迟.
好吧,我找到了一种方法来规避我观察到的问题,但如果有人可以就我的代码可能出错的地方分享一些想法,我会很高兴.我可以通过某种方式配置WebClient实例以某种方式实现相同的效果吗?
我正在使用此代码下载文件
private WebClient client;
client = new WebClient();
if (isBusy)
{
client.CancelAsync();
isBusy = false;
this.downloadButton.Text = "Download";
}
else
{
try {
Uri uri = new Uri(urlTextBox.Text);
this.downloadProgressBar.Value = 0;
client.Headers.Add("User-Agent: Other");
client.DownloadFileAsync(uri, "test.csv.zip");
this.downloadButton.Text = "Cancel";
isBusy = true;
}
catch (UriFormatException ex) {
MessageBox.Show(ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误错误
Download Not Complete: The remote server returned an error: (403) Forbidden.
Run Code Online (Sandbox Code Playgroud)
我不知道它为什么会来.
但是当我使用uri在免费下载管理器中下载它的工作
我添加了这一行
client.Headers.Add("User-Agent: Other");
Run Code Online (Sandbox Code Playgroud)
但它仍然无法正常工作.
如果有人能帮助我,我们将非常感激.
提前致谢.
我想我找到了一种通过使用WebClient.UploadFile而不是HttpWebRequest来简化我的代码的方法,但我最终在服务器端获得了一个太短且损坏的几十个字节的文件.知道bug在哪里吗?
谢谢
使用HttpWebRequest(工作正常):
HttpWebRequest req = (HttpWebRequest)HttpWebRequest
.Create("http://" +
ConnectionManager.FileServerAddress + ":" +
ConnectionManager.FileServerPort +
"/binary/up/" + category + "/" +
Path.GetFileName(filename) + "/" + safehash);
req.Method = "POST";
req.ContentType = "binary/octet-stream";
req.AllowWriteStreamBuffering = false;
req.ContentLength = bytes.Length;
Stream reqStream = req.GetRequestStream();
int offset = 0;
while (offset < ____)
{
reqStream.Write(bytes, offset, _________);
_______
_______
_______
}
reqStream.Close();
try
{
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
}
catch (Exception e)
{
_____________
}
return safehash;
Run Code Online (Sandbox Code Playgroud)
使用WebClient(服务器端的损坏文件): …
我到处寻找答案,但迄今为止没有回答我的问题.对于我正在制作的应用程序,我需要从Bukkit.org获取最新的XML工件.现在,如果我手动(通过我的浏览器)保存XML工件然后将其加载到我的程序中,它可以正常工作,给我预期格式化的XML文件.
但是,如果我使用WebClient访问该文件,我会遇到问题,因为Webclient下载的文件甚至不是XML格式.我在下面放了一个测试用例.
static void manualLoad()
{
//local copy
XDocument doc = XDocument.Load("artifacts.xml");
var lol = doc.Descendants("build_number");
foreach (XElement e in lol)
{
Console.WriteLine(e.Value); //correct output
}
}
static void onlineLoad()
{
WebClient client = new WebClient();
//save to local project folder
client.DownloadFile(new Uri("http://dl.bukkit.org/api/1.0/downloads/projects/bukkit/artifacts/")
, "C:\\...\\XMLTest\\XMLTest\\bin\\Debug\\lol.xml");
XDocument doc = XDocument.Load("lol.xml"); //error thrown!
}
static void Main(string[] args)
{
manualLoad(); //works!
onlineLoad(); //throws XMLexception: Data at root level is invalid.
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
我试图访问的XML工件是:Bukkit.org XML Artifact
我正在开发一个使用定时Web客户端的项目.类结构是这样的.
Controller =>类Form1,SourceReader,ReportWriter,UrlFileReader,HTTPWorker,TimedWebClient的主要主管.
HTTPworker是在给出url时获取页面源的类.TimedWebClient是处理WebClient超时的类.这是代码.
class TimedWebClient : WebClient
{
int Timeout;
public TimedWebClient()
{
this.Timeout = 5000;
}
protected override WebRequest GetWebRequest(Uri address)
{
var objWebRequest = base.GetWebRequest(address);
objWebRequest.Timeout = this.Timeout;
return objWebRequest;
}
}
Run Code Online (Sandbox Code Playgroud)
在HTTPWorker中我有
TimedWebClient wclient = new TimedWebClient();
wclient.Proxy = WebRequest.GetSystemWebProxy();
wclient.Headers["Accept"] = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
wclient.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center …Run Code Online (Sandbox Code Playgroud) 好了,所以我一直在阅读了大量和最好的方式工作使用async methods和task,等我相信我(主要)了解它,但我要检查,以确保.
我确实开始async wrappers使用同步任务,Task.Run()但最近读到,只有拥有一个sync method并让应用程序确定是否需要通过使用Task.Run()自身将其推送到另一个线程更好,这是有道理的.但是,我也读过这个例外是自然而然的async functions.我不知道我完全理解"自然异步",但作为一个基本的了解似乎.NET框架的方法,提供async methods的naturally async和WebClient.DownloadFile/DownlodFileAsync是其中之一.
所以我有两种方法,如果有人愿意提供反馈,我想测试我的理解,并确保这是正确的.
方法一是在本地操作系统上移动一些文件,看起来像这样(伪代码):
Public static void MoveStagingToCache()
{
...do some file move, copy, delete operations...
}
Run Code Online (Sandbox Code Playgroud)
第二个看起来像这样(伪代码):
Public static void DownloadToCache()
{
...do some analysis to get download locations...
using (var wc = new WebClient())
{
wc.DownloadFile(new Uri(content.Url), targetPath);
}
...do other stuff...
}
Run Code Online (Sandbox Code Playgroud)
所以我的理解如下.第一种方法应保持原样,因为没有文件操作方法具有async versions,因此不太可能async.调用者只需调用MoveStagingToCache()运行同步或调用Task.Run(()=>MoveStagingToCache()) …
webclient ×10
c# ×7
.net ×2
winforms ×2
async-await ×1
compression ×1
document ×1
download ×1
downloadfile ×1
exception ×1
file-upload ×1
gzip ×1
httpresponse ×1
progress-bar ×1
proxy ×1
sharepoint ×1
silverlight ×1
timeout ×1
wcf ×1
xml ×1