Bug in Clientpost.cs?

From: Jensen Bredhal (first.last_at_yahoo.com)
Date: 11/17/04


Date: Wed, 17 Nov 2004 09:42:50 +0100


Hello,

I have copied and paste the following sample code from VS.Net MSDN sample
code:
But i can not compile it . I'm getting the following error :

...\ConsoleApplication1\Class1.cs(68): The type or namespace name
'HttpUtility' does not exist in the class or namespace 'System.Web' (are you
missing an assembly reference?)

HttpUtility' belongs to the namespace System.web according to the doc.

So i do not understand why the code can not compile.

Any comment will be appreciated

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Web;

class ClientPOST {
    public static void Main(string[] args) {

        if (args.Length < 1) {
            showusage();
        } else {
            if (args.Length < 2 ) {
                getPage(args[0], "s1=food&s2=bart");
            } else {
                getPage(args[0], args[1]);
            }
        }

        Console.WriteLine();
        Console.WriteLine("Press any key to continue...");
        Console.ReadLine();

        return;
    }

    public static void showusage() {
        Console.WriteLine("Attempts to POST into to a URL");
        Console.WriteLine();
        Console.WriteLine("Usage:");
        Console.WriteLine("ClientPOST URL [postdata]");
        Console.WriteLine();
        Console.WriteLine("Examples:");
        Console.WriteLine("ClientPOST http://www.microsoft.com
s1=food&s2=bart");
    }

    public static void getPage(String url, String payload) {
        WebResponse result = null;

        try {

            WebRequest req = WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
        StringBuilder UrlEncoded = new StringBuilder();
        Char[] reserved = {'?', '=', '&'};
        byte[] SomeBytes = null;

        if (payload != null) {
        int i=0, j;
        while(i<payload.Length){
            j=payload.IndexOfAny(reserved, i);
            if (j==-1){
            UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,
payload.Length-i)));
            break;
            }
            UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,
j-i)));
            UrlEncoded.Append(payload.Substring(j,1));
            i = j+1;
        }
        SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
                req.ContentLength = SomeBytes.Length;
                Stream newStream = req.GetRequestStream();
        newStream.Write(SomeBytes, 0, SomeBytes.Length);
                newStream.Close();
            } else {
                req.ContentLength = 0;
            }

            result = req.GetResponse();
            Stream ReceiveStream = result.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            StreamReader sr = new StreamReader( ReceiveStream, encode );
            Console.WriteLine("\r\nResponse stream received");
            Char[] read = new Char[256];
            int count = sr.Read( read, 0, 256 );
            Console.WriteLine("HTML...\r\n");
            while (count > 0) {
                String str = new String(read, 0, count);
                Console.Write(str);
                count = sr.Read(read, 0, 256);
            }
            Console.WriteLine("");
        } catch(Exception e) {
            Console.WriteLine( e.ToString());
            Console.WriteLine("\r\nThe request URI could not be found or was
malformed");
        } finally {
            if ( result != null ) {
                result.Close();
            }
        }
    }
}



Relevant Pages

  • bug in clientpost.cs?
    ... I have copied and paste the following sample code from VS.Net MSDN sample ... 'HttpUtility' does not exist in the class or namespace 'System.Web' (are you ... So i do not understand why the code can not compile. ... class ClientPOST { ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: I really hate .NET especially inside Delphi
    ... in Delphi.NET terms this is a package (yeah, yeah, I don't know why ... to a BPL it will compile to a dll, ... will see a namespace called TimsTest with TTestClass in it, ... in a Delphi app in the Uses class you would put ...
    (borland.public.delphi.non-technical)
  • Re: custom namespaces
    ... and for which I think I must need to compile ... it so that the code behind can actually see/find reference to the custom ... I'm trying to create custom a namespace for use ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Weird bug in VS.NET 2003 IDE
    ... > From time to time, when I save my code, the IDE decides to add lines to ... > End Sub ... > End Namespace ... > the IDE adding all these lines is that the program fails to compile, ...
    (microsoft.public.vsnet.ide)
  • Re: How to pass information, classes between forms in Windows Application mode [ FINALLY!]
    ... declarations, it will compile. ... [that Namespace only gets rid of ambiguity in class names, ...
    (microsoft.public.dotnet.languages.csharp)