Form POST

B

Ben

Hi

I need to design a form that I can programmatically post using this code
from a VB .NET Windows Application:

Dim web As New System.Net.WebClient()

web.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

Dim d As Byte() = System.Text.Encoding.ASCII.GetBytes("data=MYDATA")
Dim res As Byte() = web.UploadData("strHttpLocation & "sync.aspx", "POST",
d)

Console.Write(System.Text.Encoding.ASCII.GetString(res))


My current form design is like this, the data being posed by an the
asp:button id="post". What changes do I need to make to enable this post
method?

Thanks
B

<HTML>
<HEAD>
<title>sync</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:textbox id="txtCmd" style="Z-INDEX: 101; LEFT: 64px; POSITION:
absolute; TOP: 120px" runat="server" Width="385px">
</asp:textbox>
<asp:textbox id="txtData" style="Z-INDEX: 102; LEFT: 56px; POSITION:
absolute; TOP: 176px" runat="server" Width="384px" Height="288px"
TextMode="MultiLine">
</asp:textbox>
<asp:button id="post" style="Z-INDEX: 103; LEFT: 384px; POSITION: absolute;
TOP: 488px" runat="server" Text="Button" CommandName="POST">
</asp:button>
</form>
</body>
</HTML>

ON CLICK
Private Sub post_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles post.Click

End sub
 
J

Joerg Jooss

Ben said:
Hi

I need to design a form that I can programmatically post using this
code from a VB .NET Windows Application:

Dim web As New System.Net.WebClient()

web.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

Dim d As Byte() = System.Text.Encoding.ASCII.GetBytes("data=MYDATA")
Dim res As Byte() = web.UploadData("strHttpLocation & "sync.aspx",
"POST", d)

Console.Write(System.Text.Encoding.ASCII.GetString(res))


My current form design is like this, the data being posed by an the
asp:button id="post". What changes do I need to make to enable this
post method?

You should post the page's ViewState as well. Fetch the page first,
parse the hidden field "__VIEWSTATE" and include that in your POST data.

Cheers,
 
B

Ben

Hi

How what do i need to parse out of the viewstate?

For example:
<input type="hidden" name="__VIEWSTATE" value="dDwxODY1ODY5NDU3O3Q8O2w8=" />
Would I post:
"VIEWSTATE=dDwxODY1ODY5NDU3O3Q8O2w8="
or
"VIEWSTATE=dDwxODY1ODY5NDU3O3Q8O2w8"

Also how to I execute the On Click event of the Post Button?

Thanks

B
 
J

Joerg Jooss

Ben said:
Hi

How what do i need to parse out of the viewstate?

For example:
<input type="hidden" name="__VIEWSTATE"
value="dDwxODY1ODY5NDU3O3Q8O2w8=" /> Would I post:
"VIEWSTATE=dDwxODY1ODY5NDU3O3Q8O2w8="
or
"VIEWSTATE=dDwxODY1ODY5NDU3O3Q8O2w8"

The former, i.e. the entire string, regardless how funny it looks ;-)

Also how to I execute the On Click event of the Post Button?

You don't. Simply mimic what parameters are passed if the Post Button
was clicked. You can use a debugging proxy like Fiddler
(www.fiddlertool.com) to view the HTTP messages.

Cheers,
 
B

Ben

Hi

I have parsed the viewstate and am now posing this data, but this does not
work. I get a response from the server but it has not posted the data.

Any ideas?

Thanks B

strPostData =
"ViewState=dDw0NzU3NTEyMjk7Oz61Iji6mRkWE2+pXtw1ci5eFtlmFA==@txtData=INSERT
INTO table (fieldname) VALUES ('TESTING')&post=submit"

gobjWebClient.Headers.Add("Content-Type",
"application/x-www-form-urlencoded")

byteResponse = gobjWebClient.UploadData(strURL, "POST",
Encoding.ASCII.GetBytes(strPostData))

TextBox1.Text = System.Text.Encoding.ASCII.GetString(byteResponse)


sync.aspx:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="sync.aspx.vb"
Inherits="myweb.sync"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>sync</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:textbox id="txtCmd" style="Z-INDEX: 101; LEFT: 56px; POSITION:
absolute; TOP: 120px" runat="server"
Width="385px"></asp:textbox><asp:textbox id="txtData" style="Z-INDEX:
102; LEFT: 56px; POSITION: absolute; TOP: 176px" runat="server"
Width="384px" Height="288px"
TextMode="MultiLine"></asp:textbox><asp:button id="post" style="Z-INDEX:
103; LEFT: 384px; POSITION: absolute; TOP: 488px" runat="server"
Text="Button" CommandName="POST"></asp:button>
<asp:Literal id="litResponse" runat="server"></asp:Literal>
</form>
</body>
</HTML>
 
B

Ben

Hi

I have go this working by looking at the data from the Fiddler

I only seem to have two problems.

Firstly one problem was that the data I was posting was in format below, the
data needed to be in teh format below that:

INSERT INTO tblMatters (matterdescription) VALUES 'description'
INSERT+INTO+tblMatters+%28matterdescription%29+VALUES+%28%27description%27%29

Is there a quick way to make this conversion?

Also I need to run this application through a proxy server.

Thanks
B

Ben said:
Hi

I have parsed the viewstate and am now posing this data, but this does not
work. I get a response from the server but it has not posted the data.

Any ideas?

Thanks B

strPostData =
"ViewState=dDw0NzU3NTEyMjk7Oz61Iji6mRkWE2+pXtw1ci5eFtlmFA==@txtData=INSERT
INTO table (fieldname) VALUES ('TESTING')&post=submit"

gobjWebClient.Headers.Add("Content-Type",
"application/x-www-form-urlencoded")

byteResponse = gobjWebClient.UploadData(strURL, "POST",
Encoding.ASCII.GetBytes(strPostData))

TextBox1.Text = System.Text.Encoding.ASCII.GetString(byteResponse)


sync.aspx:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="sync.aspx.vb"
Inherits="myweb.sync"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>sync</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:textbox id="txtCmd" style="Z-INDEX: 101; LEFT: 56px; POSITION:
absolute; TOP: 120px" runat="server"
Width="385px"></asp:textbox><asp:textbox id="txtData" style="Z-INDEX:
102; LEFT: 56px; POSITION: absolute; TOP: 176px" runat="server"
Width="384px" Height="288px"
TextMode="MultiLine"></asp:textbox><asp:button id="post" style="Z-INDEX:
103; LEFT: 384px; POSITION: absolute; TOP: 488px" runat="server"
Text="Button" CommandName="POST"></asp:button>
<asp:Literal id="litResponse" runat="server"></asp:Literal>
</form>
</body>
</HTML>
 
J

Joerg Jooss

Ben said:
Hi

I have go this working by looking at the data from the Fiddler

I only seem to have two problems.

Firstly one problem was that the data I was posting was in format
below, the data needed to be in teh format below that:

INSERT INTO tblMatters (matterdescription) VALUES 'description'
INSERT+INTO+tblMatters+%28matterdescription%29+VALUES+%28%27descriptio
n%27%29

That's plain URL encoding. There are several ways to do that. Check out
System.Web.HttpUtility or System.Uri.
Also I need to run this application through a proxy server.

HttpWebRequest has a Proxy property... it's all on MSDN.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top