Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:
C#
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?
Posted
Updated 6-Sep-12 10:30am
v2
Comments
db7uk 6-Sep-12 17:04pm    
could you not do this as part of a webservice call? Your service would reside in the website and external calls communicated with that. Therefore your service would be responsible to the file creation?
Sergey Alexandrovich Kryukov 6-Sep-12 17:45pm    
It could be a Web service or just a site -- but it needs some sever-side programming and sending the actual request from the client side. I'm not sure OP has a clue on that. Please see my answer.
--SA
Kschuler 6-Sep-12 17:29pm    
I agree with db7uk. Use a webservice.
Sergey Alexandrovich Kryukov 6-Sep-12 17:46pm    
It could be a Web service or just a site -- and OP's thing is the site. No matter what -- it needs some sever-side programming and sending the actual request from the client side.
Please see my answer.
--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 class System.Net.HttpWebRequest, create an instance through the factory method as 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
 
Share this answer
 
Comments
db7uk 6-Sep-12 17:53pm    
yep agree. I guess I was being a bit vague.
Sergey Alexandrovich Kryukov 7-Sep-12 2:13am    
Thank you for understanding.
--SA
C#
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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900