473,406 Members | 2,698 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,406 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 1800

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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.