Posts tagged ‘http’

C# download web page

I’m sure you had the need sometime to download the content of a web page to be able to analyze it or work with the content. Here is a code snippet just for you, we will use simple http GET for the specified URL.

public static string getWebPage(string psUrl)
{
    WebResponse result = null;
    string sRet = string.Empty;

    try
    {
        WebRequest req = WebRequest.Create(psUrl);
        req.Method = “GET”;
        req.Timeout = 3 * 1000// 3 secs
        // Explicit no caching, usually this is the default
        req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);

        // Has to process the results if the responding service is spitting it out
        result = req.GetResponse();
        Stream ReceiveStream = result.GetResponseStream();
        Encoding encode = System.Text.Encoding.GetEncoding(“utf-8”);
        StreamReader sr = new StreamReader(ReceiveStream, encode);
        // in case the caller is interested
        sRet = sr.ReadToEnd();
    }
    finally
    {
        if (result != null) result.Close();
    }

    return sRet;
}

Apache http access for SVN Repository

I had to add http access for our SVN repository at work, which was happily serving on the default SVN port. However after changing to our new network the SVN port has been shut down and the IT guys are in no hurry to open it up. I thought I remembered Apache can be chained with SVN for http access, went looking and sure enough. So here is a quick step tutorial with minimum configuration if you need to do the same.



1 – Download and install Apache 2.2
http://httpd.apache.org/download.cgi
I’m installing at c:\apps\apache2.



2 – Copy modules from Subversion to Apache modules
c:\apps\subversion\bin\mod_dav_svn.so
c:\apps\subversion\bin\mod_authz_svn.so
to
c:\apps\apach2\modules

and
c:\apps\subversion\bin\libdb44.dll
c:\apps\subversion\bin\intl3_svn.dll
to
c:\apps\apache2\bin
if needed



3 – Modify the Apache httpd.conf to load the SVN modules
Add the following two lines to your
c:\apps\apache2\conf\httpd.conf
file, just look for all of the lines that start with LoadModule, put them at the end of the list

LoadModule dav_svn_module modules/mod_dav_svn.so
LoadModule authz_svn_module modules/mod_authz_svn.so

Now un-comment this line in httpd.conf, it will look like this
LoadModule dav_module modules/mod_dav.so



4 – Add your svn location info to httpd.conf
At the very bottom of your httpd.conf file add these lines:

<Location /svn>
DAV svn
SVNParentPath  c:\apps\subversion\svn_repos

AuthType Basic
AuthName "Subversion Repository"
AuthUserFile conf/password.pass
Require valid-user
</Location>



5 – Create an Apache password file
To allow Apache to handle authentication for you, you’ll need to create a password file. Open a command prompt,
cd c:\apps\apache2\bin
then
htpasswd –c passwords.pass newuser

Enter the password for newuser, to add additional users

htpasswd passwords.pass anotherUser

When you have added all the users copy the passwords.pass file over to the
c:\apps\apache2\conf
directory



6 – Start the Apache service from Services
If you get errors open dos window and run Apache manually
c:\apps\apache2\bin\httpd.exe
You should see the errors reported.



7 – Access your repository over http
With Tortiese goto the repo browser, enter the appropriate path, something like the following.
http:///10.0.0.1/svn/yourRepo
You will be prompted for the user and password and should be all good to go. You should also be able to open the location in a web browser.



This quick step guide is based on the more comprehensive guide at inetsolution. That guide is great and covers setup of SVN+Apache, SSL access and group permissions.

C# http authenticate

Sometimes URL requests are authenticated by the server your running against. For example if you want to update your Twitter status. Let’s take a look how we can do that easy.

using System.Net;
try
{
    string sURL = “http://twitter.com/statuses/update.xml?status=” + sText;
    // Create the web request
    HttpWebRequest request = WebRequest.Create(sURL) as HttpWebRequest;

    // Add authentication to request
    request.Credentials = new NetworkCredential(“myaccount”, “mypassword”);
    request.PreAuthenticate = true;
    request.Method = “POST”;

    // Get response
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        // Get the response stream
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            // Console application output
            StringBuilder body = new StringBuilder(reader.ReadToEnd());
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

Servlet Authenticator problem – part 2

I had a problem at the hosting place with an authenticated http request. Now 5 days later it’s finally solved.  5 days mind you is a long time.  It goes something like this.

1. Send in a support request.

2. Wait until the next day.

3. Get answer from a junior person claiming they don’t understand the problem well enough.

4. Repeat

Until finally a senior person or a “tech” takes a look at the problem and solves it.   In other words 5 emails and 5 days later my problem is finally solved, not good.  Yes my place is cheap and has a nice cPanel were you can do most of the administration yourself.  Setup MySql, subdomains, email forwards etc, but if you need help from support…

Anyway, the java servlet is now running, it will pull up my tweet feed and read it to you on the phone.  You can call it here 720-897-8900 or skype call it +990009369990026351

And on a less happier note, LinuxHaters is closing shop.  I’m thinking about doing a petition, any takers ?