HttpWebRequest C# to VB equiv.

M

mplutodh1

I am not up with all the C# as I usually use VB, could someone explain
to me how I would accomplish this in VB? Thanks!

http://www.netomatix.com/HttpPostData.aspx


private void OnPostInfoClick(object sender, System.EventArgs e)
{
string strId = UserId_TextBox.Text;
string strName = Name_TextBox.Text;

ASCIIEncoding encoding=new ASCIIEncoding();
string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);

// Prepare web request...
HttpWebRequest myRequest =

(HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}
 
M

Mythran

mplutodh1 said:
I am not up with all the C# as I usually use VB, could someone explain
to me how I would accomplish this in VB? Thanks!

http://www.netomatix.com/HttpPostData.aspx


private void OnPostInfoClick(object sender, System.EventArgs e)
{
string strId = UserId_TextBox.Text;
string strName = Name_TextBox.Text;

ASCIIEncoding encoding=new ASCIIEncoding();
string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);

// Prepare web request...
HttpWebRequest myRequest =

(HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}
Something like the following:

Private Sub OnPostInfoClick(ByVal eender As Object, ByVal e As EventArgs)
Dim strId As String = UserId_TextBox.Text
Dim strName As String = Name_TextBox.Text

Dim encoding As System.Text.ASCIIEncoding = New
System.Text.ASCIIEncoding()
Dim postData As String = "userid=" & strId
postData &= "&username=" & strName
Dim data As Byte() = encoding.GetBytes(postData)

' Prepare web request...
Dim myRequest As HttpWebRequest = DirectCast( _
WebRequest.Create(http://localhost/MyIdentity/Default.aspx), _
HttpWebRequest _
)

With myRequest
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = data.Length
End With

Dim newStream As System.IO.Stream = myRequest.GetRequestStream()

' Send the data.
newStream.Write(data, 0, data.Length)
newStream.Close()
End Sub

HTH,
Mythran
 
M

mplutodh1

So I think I converted it to some extent however the data is not being
posted to the paypal site like it should. Here is what I have:


Dim encoding As ASCIIEncoding = New ASCIIEncoding
Dim postData As String =
"cmd=_xclick&[email protected]&item_name=Registration&item_number="
& item_number & "&amount=" & amount &
"&return=http://website.com/temp/racing.aspx...=true&no_note=1&currency_code=USD&first_name="
& first_name & "&last_name=" & last_name & "address1=" & address1 &
"&city=" & city & "&state=" & zip & "&zip=" & zip & "&email=" & email &
"&night_phone_a=" & night_phone_a &""
Dim data As Byte() = encoding.GetBytes(postData)
Dim myRequest As HttpWebRequest =
CType(WebRequest.Create("https://www.paypal.com/cgi-bin/webscr"),
HttpWebRequest)
myRequest.Method = "POST"
myRequest.ContentType = "application/x-www-form-urlencoded"
myRequest.ContentLength = data.Length
Dim newStream As Stream = myRequest.GetRequestStream
newStream.Write(data, 0, data.Length)
newStream.Close


Any ideas on why it isn't posting to paypal?
 
J

Juan T. Llibre

Try this on for size :

Private Sub OnPostInfoClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strId As String = UserId_TextBox.Text
Dim strName As String = Name_TextBox.Text
Dim encoding As ASCIIEncoding = New ASCIIEncoding
Dim postData As String = "userid=" + strId
postData += ("&username=" + strName)
Dim data As Byte() = encoding.GetBytes(postData)
Dim myRequest As HttpWebRequest = CType(WebRequest.Create("http://localhost/MyIdentity/Default.aspx"), HttpWebRequest)
myRequest.Method = "POST"
myRequest.ContentType = "application/x-www-form-urlencoded"
myRequest.ContentLength = data.Length
Dim newStream As Stream = myRequest.GetRequestStream
newStream.Write(data, 0, data.Length)
newStream.Close
End Sub

Translation to VB.NET courtesy of http://www.developerfusion.com/utilities/convertcsharptovb.aspx



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
M

mplutodh1

Thanks guys, I think I have the conversion done, but my hope is the
page will redirect after posting to that form, I am using this request
to preload fields in PayPal. Any ideas on why its not working?
 
G

Guest

Courtesy of Instant VB (Demo at www.instantvb.com):

Private Sub OnPostInfoClick(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim strId As String = UserId_TextBox.Text
Dim strName As String = Name_TextBox.Text

Dim encoding As ASCIIEncoding = New ASCIIEncoding()
Dim postData As String="userid=" & strId
postData &= ("&username=" & strName)
Dim data As Byte() = encoding.GetBytes(postData)

' Prepare web request...
Dim myRequest As HttpWebRequest =
CType(WebRequest.Create("http://localhost/MyIdentity/Default.aspx"),
HttpWebRequest)

myRequest.Method = "POST"
myRequest.ContentType="application/x-www-form-urlencoded"
myRequest.ContentLength = data.Length
Dim newStream As Stream=myRequest.GetRequestStream()
' Send the data.
newStream.Write(data,0,data.Length)
newStream.Close()
End Sub

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter
 

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