473,289 Members | 1,929 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,289 software developers and data experts.

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 1795

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

Similar topics

15
by: Thomas Scheiderich | last post by:
I am trying to understand Session variables and ran into a question on how they work with data that is passed. I have an HTM file that calls an ASP file and sends the name either by GET or POST....
3
by: Geoff | last post by:
I have been asked to add postal rates to a web page shopping cart checkout procedure. After contacting the post office I discovered that they used XML to receive postal rate requests and send back...
6
by: Phillip N Rounds | last post by:
I have a webform, from which I have to submit info to another site. Their instructions are to have a html form, with the following as the submit: <form method="post"...
4
by: Evgeny Zoldin | last post by:
Hi ALL. I would like to download file from some web-server that requires Basic-Authetication, change somthing in the file and upload it back to the server. I can download the file and proceed...
1
by: Arfeen | last post by:
Hi All, I need help again ..... I have an asp.net web page which I hit using the "HTTP POST" method. My ASP.NET page is a basic hello world example with the following code: private void...
6
by: majik92 | last post by:
Hello, I'm in the process of rewriting a program I wrote in LUA to VB.NET. So, I am still learning the language (VB). I just have a question... I have a windows form called "mainPage". I want...
1
pntkiran
by: pntkiran | last post by:
Hi all, I am implementing a http client in c++. I want to write a method to send the POST request to server. How will i do that? what is the format for the request to server send by POST method? ...
0
by: LucLim | last post by:
Hi I'm new to ASP and will greatly appreciate any assistance offered. Firstly, I will like to know if it is possible to send both an XML file and an image via the HTTP Post method to a web...
6
by: neerom | last post by:
Hi, I am newbie in xslt. I have some problem in my current project. I have to call web service from xslt. I have to use HTTP Post method to access webservice. I have got a guideline but...
5
by: NitinSawant | last post by:
Hello, I'm beginner to php (actually i'm java/jsp developer), What i'm trying to do is Accept parameters from the HTML file and write them to a newly created file using php, I wrote...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.