C# webclient Issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: C# webclient Issue

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Talking C# webclient Issue

    Hello everyone, I'm new to C# so please bare with me. I've not done any programming before and just decided to take this little task on myself since a friend of mine said it was a relatively easy project to do in Visual Basic. I've learned a lot over the past week thanks to this and the "Search" function and Google.

    So, my work uses a VPN for me to be able to remotely log in whenever I need to. However the configuration changes frequently. So, instead of downloading a file every day and overwriting the existing configuration file manually I wanted it to be automated.

    So far, here's what I have:

    Code:
    using System;
    using System.Linq;
    using System.IO;
    using System.Net;
    using System.Windows.Forms;
    using System.Diagnostics;
    
    namespace Config_Updater
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                Uri uri = new Uri("https://myupdatedconfigfile.com");
                string filename = @"C:\Program Files\OpenVPN\config";
                {
                begin:
                    Process[] pname = Process.GetProcessesByName("OpenVPN"); 
    
                    if (pname.Length == 1)
                    {
                        //string processName = "OpenVPN";
    
                        foreach (Process proc in Process.GetProcessesByName("OpenVPN"))
                            proc.Kill();
                        goto begin;
                    }
                    try
                    {
                        if (File.Exists(filename))
                        {
                            File.Delete(filename);
                        }
                        else
                        {
                            WebClient wc = new WebClient();
                            wc.DownloadFile(uri, filename);
                         }
                     finally
                    {
                        Application.Exit();
                            } 
                  }
                }
        }
    }
    So, I want the program to accomplish several things.
    1. If the OpenVPN process is already running I want it to close it.
    2. I want it to identify and delete the old config file to ensure it is completely removed.
    3. Download the most current Config file for OpenVPN and then end application.



    However, when I debug this program I get a WebClient was unable to initialize type error. Am I missing a step completely? Did I not include a specific "using System"?

    Any and all help you can provide me with would be very much appreciated.

  2. #2
    Join Date
    Jul 2012
    Posts
    90

    Re: C# webclient Issue

    Just guessing but...

    Since the URL "https://myupdatedconfigfile.com" indicates SSL, you should check with the web master to see if the server requires your web client to send credentials (some do - and that is really what the exception is indicating). If it is the case that the server requires credentials, you need to find out if it requires a client cert (likely if you VPN using a CAC card) or something else.

    Once you have the particulars of what the server requires to allow you to establish a connection, you'll need to google how to present that type of credentials using the WebClient.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center