POST to page using asp.net

C

csgraham74

Just a quick one - im trying to post values to a page using a method
below. im coding in asp.net vb. i was wondering how i also redirect to
the page as well as post the values at the same time. e.g. in html it
would be something like

<form action=https://www.text.cgi method=post>
<input type=visible name="val1" value="<%=val1%>">
<input type=visible name="val2" value="<%=val2%>">
<input type=submit value="Proceed to server" id="Submit1"
language="javascript" onclick="return Submit1_onclick()">
</form>


posting in asp.net - i think ?????? any help appreciated


Public Shared Function readHtmlPage(ByVal postData As String) As
String
Dim result As String = ""
Dim strPost As String = postData
Dim myWriter As StreamWriter

Dim objRequest As HttpWebRequest = WebRequest.Create("https://
www.test.cgi")
objRequest.Method = "POST"
objRequest.ContentLength = strPost.Length
objRequest.ContentType = "application/x-www-form-urlencoded"

Try
myWriter = New StreamWriter(objRequest.GetRequestStream())
myWriter.Write(strPost)
Catch e As Exception
Return e.Message
Finally
myWriter.Close()
End Try

Dim objResponse As HttpWebResponse = objRequest.GetResponse()
Dim sr As StreamReader
sr = New StreamReader(objResponse.GetResponseStream())
result = sr.ReadToEnd()
sr.Close()

Return result
End Function
 
B

bruce barker

your postdata would be something like:

string.Format("val1={0}&val2={1}&Submit1=Proceed+to+server",
HttpUtility.UrlEncode(val1),
HttpUtility.UrlEncode(val1));

-- bruce (sqlwork.com)
 
S

sloan

I don't know if this helps or not.

But I dug up some old code:

FormPostCollection and FormPostItem is just a simple name/value pair.

As in.

EmpID=123
fpi.Key , fpi.Value

You could replace with NamedValuePairCollection or similar dictionary
object.


I just remember it taking several days to get all the nooks/crannies worked
out .




private void postDataToHttpWebRequest ( HttpWebRequest webRequest ,
Collections.FormPostCollection formPostCollec)
{


if (null != formPostCollec )
{

//byte[] data =
MeasInc.AsynchronousFramework.SerializationLib.SerializationHelper.StringToUnicodeByteArray
(PARAMTER_DS_FORM_KEY +"="+parameterDS.GetXml());



ASCIIEncoding encoding=new ASCIIEncoding();

byte[] data = encoding.GetBytes(this.buildPostString(formPostCollec));

webRequest.Method = "POST";
webRequest.ContentType="application/x-www-form-urlencoded";
//oHttpWebRequest.ContentType = "text/xml";//Does Not Work

webRequest.ContentLength = data.Length;
Stream newStream=webRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}



}








private string buildPostString ( Collections.FormPostCollection
formPostCollec)
{

StringBuilder sb = new StringBuilder();

foreach (BusinessObjects.FormPostItem fpi in formPostCollec)
{
//string postValue = Encode(Request.Form(postKey));
sb.Append( string.Format("{0}={1}&", fpi.Key , fpi.Value ));
}

return sb.ToString();
}
 

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

Top