写入某个服务器上的文本文件 [英] Write to a text file that's on some server
本文介绍了写入某个服务器上的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个程序,该程序连接到我的网站以获取数据.它只需要写文本文件.我的问题是我不知道该在哪里设置用户信息,用户名和密码,以便我的应用程序能够更改文本文件.这是我的代码:
I''m building a program that connects to my site for data. It only needs to write text file. My problem is that I don''t know where I have to set up user information, username and password so that my app would be able to change the text file. Here is my code:
Uri uri = new Uri("website");
WebRequest req = WebRequest.Create(uri);
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamWriter sw = new StreamWriter(stream);
sw.WriteLine("Something...");
sw.Close();
stream.Close();
resp.Close();
我知道我可以下载文件,对其进行编辑并比对其进行上传,但是还有什么更好的方法吗?
I know I can download file,edit it and than upload it, but is there any better way?
推荐答案
首先,您需要发送请求;但是你没有那样做.在这种情况下,您需要使用类System.Net.HttpWebRequest
,并像实际一样通过工厂方法创建一个实例:
http://msdn.microsoft.com/en-us/library/system.net. webrequest.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.net. httpwebrequest.aspx [ ^ ].
您将在上面引用的MSDN文章中找到一些代码示例.
但是最重要的是:服务器端应设计为获取请求,并将其解释为创建文件的请求(这是标准的HTTP文件上传),或根据某些自定义请求修改文件.无论您在服务器端上具有Web Service还是Web站点,它都可以通过这种方式工作.换句话说,您需要一些服务器端编程.因为这是您的站点,所以可以这样做,但是您需要为HTTP服务器配置一些服务器端脚本技术:ASP.NET,PHP,带有Python的WSGI(将其命名).
-SA
First of all, you need to send a request; but you are not doing anything like that. In this case, you need to use the classSystem.Net.HttpWebRequest
, create an instance through the factory methodas you actually do:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^].
You will find some code samples in the MSDN articles referenced above.
But most important thing is: the server side should be designed to get a request and interpret it as a request for creation of a file (which is a standard HTTP file upload) or modify the file on some custom request. It works this way, no matter if you have a Web Service or a Web site on the server end. In other words, you need some server-side programming. As this is your site, you can do it, but you need some server-side scripting technology configured for your HTTP server: ASP.NET, PHP, WSGI with Python — you name it.
—SA
private bool UploadDocumentWebClient(string FileNamePath, string DestinationUrl)
{
bool returnValue = false;
try
{
DestinationUrl = DestinationUrl.Replace(" ", "%20");
WebClient m_webClient = new WebClient();
m_webClient.Credentials = new NetworkCredential("UserName", "Password", "Domain");
m_webClient.UploadFile(DestinationUrl, "PUT", FileNamePath);
returnValue = true;
}
catch
{
}
return returnValue;
}
这篇关于写入某个服务器上的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文