[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1225
  • Last Modified:

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

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
janwrage
Asked:
janwrage
  • 9
  • 4
  • 2
  • +1
2 Solutions
 
Tuyau2poilCommented:

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
 
janwrageAuthor Commented:
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
 
karthitronCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
leakim971PluritechnicianCommented:
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
 
janwrageAuthor Commented:
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
 
janwrageAuthor Commented:
Ahh, there's a typo in the code. Must be http, not hhtp...
0
 
janwrageAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
 
janwrageAuthor Commented:
Ok, may be thats why it works sometimes...thanks for that!

But my original question is still not solved :)
0
 
Tuyau2poilCommented:
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
 
janwrageAuthor Commented:
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
 
Tuyau2poilCommented:
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
 
janwrageAuthor Commented:
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
 
Tuyau2poilCommented:
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
 
janwrageAuthor Commented:
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
 
janwrageAuthor Commented:
Although not solving my initial question, it leads me to the right way.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now