469,926 Members | 1,841 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

HTTP "POST" method doesn't really post.

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 programs can be
successfully compiled. There is no error message shown at running
also. But the httpLook confirms that the program doesn't post.
--
Here is the HTTP request acquired by the software:
---
POST /cgi-bin/login.cgi HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 15
Expect: 100-continue
Connection: Keep-Alive
Host: tw.reg.yahoo.com

--
There is supposed to be the posted data after the "Host" Header, see?
The program doesn't send anything.
--
POST /cgi-bin/login.cgi HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 15
Expect: 100-continue
Connection: Keep-Alive
Host: tw.reg.yahoo.com
<<<<<"posted data should be listed here">>>>>>>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
--
I use the IE to test the httpLook software for post method, and there
is no problem with this software. Can't someone anwser me this
question??

This program was modified from the MS. Postclient sample.

Here is the C# version.
--
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)
{
getPage("http://tw.reg.yahoo.com/cgi-bin/login.cgi",
"s1=food&s2=bart");
}
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();
}
}
}
}

---
ANd the VB.NET version:
---
Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Web
Imports Microsoft.VisualBasic

Namespace Client
Public Class ClientPOST
Public Shared Sub Main()
Dim args As String()

args = Environment.GetCommandLineArgs()

If (args.Length < 2) Then
getPage("http://tw.reg.yahoo.com/cgi-bin/login.cgi",
"sl=foo&s2=bar")
Else
If args.Length < 3 Then
getPage(args(1), "sl=foo&s2=bar")
Else
getPage(args(1), args(2))
End If
End If

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

Public Shared Sub showusage()
Console.WriteLine("Attempts to POST to a URL")
Console.WriteLine()
Console.WriteLine("Usage:")
Console.WriteLine("ClientPOST URL [postdata]")
Console.WriteLine()
Console.WriteLine("Examples:")
Console.WriteLine("ClientPOST http://www.nba.com
[s1=foo&s2=bar]")
End Sub

Public Shared Sub getPage(ByVal url As String, ByVal payload
As String)
Dim result As WebResponse

Try
Dim req As WebRequest
Dim RequestStream As Stream
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader

req = WebRequest.Create(url)
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
Dim SomeBytes() As Byte
Dim UrlEncoded As New StringBuilder
Dim reserved() As Char = {ChrW(63), ChrW(61),
ChrW(38)}

If payload <> Nothing Then
Dim i As Integer = 0
Dim j As Integer
While i < payload.Length
j = payload.IndexOfAny(reserved, i)
If j = -1 Then

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Su bstring(i,
payload.Length - i)))
Exit While
End If

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Su bstring(i, j - i)))
UrlEncoded.Append(payload.Substring(j, 1))
i = j + 1
End While
SomeBytes =
System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToSt ring())
req.ContentLength = SomeBytes.Length
RequestStream = req.GetRequestStream()
RequestStream.Write(SomeBytes, 0,
SomeBytes.Length)
RequestStream.Close()
Else
req.ContentLength = 0
End If
result = req.GetResponse()
ReceiveStream = result.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(ReceiveStream, encode)

Console.WriteLine()
Console.WriteLine("Response stream received")
Dim read(256) As Char
Dim count As Integer = sr.Read(read, 0, 256)

Console.WriteLine("HTML...")
Console.WriteLine()
Do While count > 0
Dim str As String = New String(read, 0, count)
Console.Write(str)
count = sr.Read(read, 0, 256)
Loop
Console.WriteLine("")
Catch Exc As Exception
Console.WriteLine()
Console.WriteLine("The request URI could not be found
or was malformed")
Finally
If Not Result Is Nothing Then
Result.Close()
End If
End Try
End Sub
End Class
End Namespace
Nov 20 '05 #1
0 1662

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Thomas Scheiderich | last post: by
6 posts views Thread by Phillip N Rounds | last post: by
4 posts views Thread by Evgeny Zoldin | last post: by
1 post views Thread by Arfeen | last post: by
6 posts views Thread by majik92 | last post: by
pntkiran
1 post views Thread by pntkiran | last post: by
6 posts views Thread by neerom | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.