473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Bug in Clientpost.cs?


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.Su bstring(i,
payload.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Su bstring(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();
}
}
}
}
Jul 21 '05 #1
2 1220
Add a reference to system.web.dll .
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jensen Bredhal" <fi********@yahoo.com> wrote in message
news:OW*************@TK2MSFTNGP10.phx.gbl...

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.Su bstring(i,
payload.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Su bstring(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();
}
}
}
}

Jul 21 '05 #2

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:jq********************@news4.e.nsc.no...
Add a reference to system.web.dll .


Many thanks...
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jensen Bredhal | last post by:
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):...
0
by: Owen | last post by:
Hello everyone, I am using VS.NET 2003(Trandition Chinese) Edition, and httpLook software for checking http requests. I found a problem that the following programs don't really "POST". These...
2
by: Jensen Bredhal | last post by:
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):...
19
by: Jonathan Wood | last post by:
I am trying to simulate a post to a page that is on another server and is not implemented in ASP.NET. I've found some examples for this using WebRequest. That code appears to work and the data...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.