Solved

Passing parameters to website (jsf) using post from c# or vb.net

Posted on 2011-03-07
16
1,173 Views
Last Modified: 2013-11-24
Hi!

I need to post some parameters to a website and I don't know how.

Please see example script which works. I need to do this within my application.

I have no knowledge of javascript nor html so a co-worker wrote the script for me but he doesn't know how to do this in .net.

A sample call to this is: hvv_aufruf_mit_Parametern.html?abfahrt=1&ankunft=0&start=Wedel&ziel=Barmbek&uhrzeit=14:00&datum=04.03.2011

Thanks

Jan
<html>
	<head>
	 
		<script type="text/javascript">
			
			function laden()
			{
				var params=document.URL.split("?")[1]; // Parameterwerte			
				var einzelwerte = params.split("&");			
				for(i = 0; i < einzelwerte.length; i++){
					var namen = einzelwerte[i].split("=");
					document.getElementById(namen[0]).value=namen[1];				}
			}
			
		</script>
	</head>
	<body onload="laden();">
		<form name="schnellauskunft" method="post" action="http://geofox.hvv.de/jsf/postPS.seam" target="_blank" >
			<hidden name="lang" value="de"></hidden>
			<input type="hidden" name="POST" value="true" />

			<input type="text" id="abfahrt" name="a" value="" />
			<input type="text" id="ankunft" name="a" value="" />

			<input type="text" id="start" name="sn" value=""/>
			<input type="text" id="ziel" name="zn" value=""/>
			<input type="text" id="uhrzeit" name="z" value=""/>
			<input type="text" id="datum" name="d" value=""/>

		</form>
		
		<script>
			document.schnellauskunft.submit();
			window.close();
		</script>
	</body> 
</html>

Open in new window

0
Comment
Question by:janwrage
  • 9
  • 4
  • 2
  • +1
16 Comments
 
LVL 4

Expert Comment

by:Tuyau2poil
Comment Utility

no need javascript : you can create asp textbox control in aspx page : abfahrt.text, start.text and so on.

and in codebehind, first you create URL string like this :

string URL = "hvv_aufruf_mit_Parametern.html?abfahrt=" + abfahrt.text + "&start=" + start.text & ... (and so on)

and second you send URL :

Response.Redirect(URL);


inversely, when you received page, you can retrieve parameters with 'request' method in codebehind :

example :

 abfahrt.text=Request["abfahrt"];
0
 
LVL 1

Author Comment

by:janwrage
Comment Utility
Hi!

Thanks for your response, but I don't want to use the above mentioned script. It's only a way that seems to work.

I need to mimic this behaviour from my winforms-application which hosts a browser control.

Maybe there's a better way to do this...please see the website: http://geofox.hvv.de

I need to fill the required fields and display the results.

Thanks and regards
Jan
0
 
LVL 5

Expert Comment

by:karthitron
Comment Utility
Hi, you have to do similar code as you provided in above example. The thing is, in your .aspx page using "form" tag, you have to set the

method = "post"  (Mandatory - Used to post the values in the input controls available in current page to target page)
action="REDIRECT OR TARGET URL / (PAGE WITHIN THE WEBSITE) WHERE YOU WANT TO READ THE INPUT VALUES" (Mandatory - Used to redirect to this page after clicking submit button) (On Submit button's click event we have to do validatie the input controls and send true or false. If true is sent, page will be posted to the target web page as mentioned in "action" attribute)

target ="_blank" (Optional - To determine, the target page should open in a new blank page or self etc.."

Thats it.. Here I'm pasting the same code as mentioned in your sample:
<form name="yourFormName" method="post" action="YOUR_TARGET_PAGE.aspx" target="_blank" >

In your "PAGE1.aspx", you have to place the input controls as mentioned in your sample.

and in your YOUR_TARGET_PAGE.aspx, using Request object, you can access the input control values submitted from your "PAGE1.aspx".
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
Comment Utility
A small update of your page :


<html>
	<head>
	 
		<script type="text/javascript">
			
			function laden()
			{
				var params=document.URL.split("?")[1]; // Parameterwerte			
				var einzelwerte = params.split("&");			
				for(i = 0; i < einzelwerte.length; i++){
					var namen = einzelwerte[i].split("=");
					document.getElementById(namen[0]).value=namen[1];
 				}
			}
			
		</script>
	</head>
	<body onload="laden();document.schnellauskunft.submit();">
		<form name="schnellauskunft" method="post" action="http://geofox.hvv.de/jsf/postPS.seam" >
			<hidden name="lang" value="de"></hidden>
			<input type="hidden" name="POST" value="true" />

			<input type="text" id="abfahrt" name="a" value="" />
			<input type="text" id="ankunft" name="a" value="" />

			<input type="text" id="start" name="sn" value=""/>
			<input type="text" id="ziel" name="zn" value=""/>
			<input type="text" id="uhrzeit" name="z" value=""/>
			<input type="text" id="datum" name="d" value=""/>

		</form>
	</body> 
</html>

Open in new window

0
 
LVL 1

Author Comment

by:janwrage
Comment Utility
Ahh...guys, I have no .aspx page...just a stupid windows-forms-application with a web-browser control placed on it. And this control has a method .navigate (see Webbrowser-Control)

OK, so forget the script above. All I need to do is to fill in the required fields in the abovementioned page and display the result to the user. I tried the following code, but it did not work.

Thanks!
Dim postData As String = String.Format("schnellauskunft:a={0}&schnellauskunft:a={1}&schnellauskunft:sn={2}&schnellauskunft:zn={3}&schnellauskunft:z={4}&schnellauskunft:d={5}", 1, 0, "Wedel", "Barmbek", "14:00", "07.03.2011")

        Dim enc As New ASCIIEncoding

        WebBrowser1.Navigate("hhtp://geofox.hvv.de/jsf/postPS.seam", "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n")

Open in new window

0
 
LVL 1

Author Comment

by:janwrage
Comment Utility
Ahh, there's a typo in the code. Must be http, not hhtp...
0
 
LVL 1

Author Comment

by:janwrage
Comment Utility
Hey leakim971,

where's the difference to my script?

It seems that your submit in the onLoad is better than the way it is done in my script. With my script, the website is not always showing the correct results, with yours its working every time...
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
we submit the page after populate the fields in the form.
else you generaly submit the form without having time to populate the fields
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:janwrage
Comment Utility
Ok, may be thats why it works sometimes...thanks for that!

But my original question is still not solved :)
0
 
LVL 4

Expert Comment

by:Tuyau2poil
Comment Utility
with vb.net, you must create a webrequest object : example show on msdn :

you must put all the page content into a string and submit it to webrequest (see "put your page content here" in following example).


Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
    Public Class WebRequestPostExample

        Public Shared Sub Main()
            ' Create a request using a URL that can receive a post.
            Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ")
            ' Set the Method property of the request to POST.
            request.Method = "POST"
            ' Create POST data and convert it to a byte array.
            Dim postData As String = "put your page content here"
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
            ' Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded"
            ' Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length
            ' Get the request stream.
            Dim dataStream As Stream = request.GetRequestStream()
            ' Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length)
            ' Close the Stream object.
            dataStream.Close()
            ' Get the response.
            Dim response As WebResponse = request.GetResponse()
            ' Display the status.
            Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
            ' Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)
            ' Clean up the streams.
            reader.Close()
            dataStream.Close()
            response.Close()
        End Sub
    End Class
End Namespace


if you work with VB, use activeX web control :

Webcontrol.Execute URL, "POST", mydata


 
0
 
LVL 1

Author Comment

by:janwrage
Comment Utility
I know that example but I don't know what to pass to the website...my data posted above seems to be incorrect.

I don't know how to create this string nor do I know what to pass and in which way...
0
 
LVL 4

Assisted Solution

by:Tuyau2poil
Tuyau2poil earned 250 total points
Comment Utility
easiest way is to past your page code into a textbox and get textbox.text in code !

if you want to create string "by code" from your actual web page content , you must change all ' " ' by ' " & chr(34) & "' , start line with ' " ' and end line with ' " & _ '

so code looks like :

MyString = "<html>" & _
"      <head>" & _
      
"      <script type=" & chr34 & "text/javascript" & chr34 & ">" & _
"      function laden()" & _
"      {" & _
"      var params=document.URL.split(" & chr34 & "?" & chr34 & ")[1];" & _      
"      var einzelwerte = params.split(" & chr34 & "&" & chr34 & ");" & _      ... and so on

after that, you can send this content to your server by using :

 Public Shared Sub Main()
            Dim request As WebRequest = WebRequest.Create("myServer")
            request.Method = "POST"
            Dim postData As String = MyString
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = byteArray.Length
            Dim dataStream As Stream = request.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()
            Dim response As WebResponse = request.GetResponse()

server response will then be catch by :

            dataStream = response.GetResponseStream()
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            Console.WriteLine(responseFromServer)
            reader.Close()
            dataStream.Close()
            response.Close()

                 

0
 
LVL 1

Author Comment

by:janwrage
Comment Utility
Hehehe...may be it's so hard to understand because of my bad explanation. English ist not my native language so I can't write exactly what I think ;)

I know how to build strings etc...but I don't want to put all that crap into a string and post it to the webserver.

There must be another way to get a result (route) from this website without using the above script. If not, I'll stick with the script and call it from my vb.net-application.
0
 
LVL 4

Expert Comment

by:Tuyau2poil
Comment Utility
OK, I try to understand (I'm french, so double translate lol !) :

in your windows form, user type required informations in textbox, press a button and get results.

but you don't want to simulate a web request and extract return informations from web response.

may be you can get concerned code behind from intranet site and rewrite it in your windows form ? (I suppose that's C# code whith sql query and so on) ?

alan



 
0
 
LVL 1

Author Comment

by:janwrage
Comment Utility
I don't have access to the involved code behind the site since it's an internet-site.

Anyways I'm using the script now 'cause I'm in a hurry with this.

Assigning the points to both of you for help.

Thanks and regards.
Jan
0
 
LVL 1

Author Closing Comment

by:janwrage
Comment Utility
Although not solving my initial question, it leads me to the right way.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now