472,971 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 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 1774

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 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.