N
Natalia
Hello,
I need to provide the ability to post file and some form elements via
our website (asp.net) to the third party website (asp page). On
http://aspalliance.com/236#Page4 - I found great advices but still
having troubles... it might some obvious error that I am making but I
just dont see it.
==================FIRST - Webclient=================================
RESULT: Exception : ProtocolError (server 500 error)
================================================================
Dim url as String = "http://www.site2post.com"
Dim q As New System.Collections.Specialized.NameValueCollection
q.Add("id", "123456789")
q.Add("merchant_pin", "987654321")
Dim wc As New System.Net.WebClient
wc.Headers.Add("Content-Type",
"application/x-www-form-urlencoded")
wc.QueryString = q
Dim responseArray As Byte() = wc.UploadFile(url, "POST",
"C:\folder\file.txt")
If responseArray.Length > 0 Then
resultString &=
System.Text.Encoding.ASCII.GetString(responseArray).ToString
Else
resultString &= "No response"
End If
=============SECOND- HTTPWebREQUEST ===========================
RESULT: can pass variables but can not pass the file.
================================================================
Dim url as String =
"http://www.site2post.com?id=123456789&merchant_pin=987654321"
Dim strFileToUse As String = "C:\folder\file.txt"
Dim st As New FileStream(strFileToUse, FileMode.Open)
Dim Tem() As Byte
ReDim Tem(st.Length)
st.Read(Tem, 0, st.Length)
st.Close()
Dim req As System.Net.HttpWebRequest
req = System.Net.WebRequest.Create(url)
'--set the standard header information
req.ProtocolVersion = HttpVersion.Version11
req.Method = "POST"
req.Accept = "*/*"
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; .NET CLR 1.0.3705)"
req.ContentType = "application/x-www-form-urlencoded"
'req.AllowAutoRedirect = False
req.ContentLength = Tem.Length
'--set additional header information
req.Headers.Add("id", "123456789")
req.Headers.Add("merchant_pin", "987654321")
' Perform the request
Dim requestStream As Stream = req.GetRequestStream()
requestStream.Write(Tem, 0, Tem.Length)
requestStream.Close()
'read in the page
Dim res As System.Net.HttpWebResponse
res = req.GetResponse()
If req.HaveResponse Then
Dim sr As System.IO.StreamReader
sr = New
System.IO.StreamReader(res.GetResponseStream())
resultString = sr.ReadToEnd
sr.Close()
End If
res.Close()
Please help.
Thanks in advance.
Natalia
I need to provide the ability to post file and some form elements via
our website (asp.net) to the third party website (asp page). On
http://aspalliance.com/236#Page4 - I found great advices but still
having troubles... it might some obvious error that I am making but I
just dont see it.
==================FIRST - Webclient=================================
RESULT: Exception : ProtocolError (server 500 error)
================================================================
Dim url as String = "http://www.site2post.com"
Dim q As New System.Collections.Specialized.NameValueCollection
q.Add("id", "123456789")
q.Add("merchant_pin", "987654321")
Dim wc As New System.Net.WebClient
wc.Headers.Add("Content-Type",
"application/x-www-form-urlencoded")
wc.QueryString = q
Dim responseArray As Byte() = wc.UploadFile(url, "POST",
"C:\folder\file.txt")
If responseArray.Length > 0 Then
resultString &=
System.Text.Encoding.ASCII.GetString(responseArray).ToString
Else
resultString &= "No response"
End If
=============SECOND- HTTPWebREQUEST ===========================
RESULT: can pass variables but can not pass the file.
================================================================
Dim url as String =
"http://www.site2post.com?id=123456789&merchant_pin=987654321"
Dim strFileToUse As String = "C:\folder\file.txt"
Dim st As New FileStream(strFileToUse, FileMode.Open)
Dim Tem() As Byte
ReDim Tem(st.Length)
st.Read(Tem, 0, st.Length)
st.Close()
Dim req As System.Net.HttpWebRequest
req = System.Net.WebRequest.Create(url)
'--set the standard header information
req.ProtocolVersion = HttpVersion.Version11
req.Method = "POST"
req.Accept = "*/*"
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; .NET CLR 1.0.3705)"
req.ContentType = "application/x-www-form-urlencoded"
'req.AllowAutoRedirect = False
req.ContentLength = Tem.Length
'--set additional header information
req.Headers.Add("id", "123456789")
req.Headers.Add("merchant_pin", "987654321")
' Perform the request
Dim requestStream As Stream = req.GetRequestStream()
requestStream.Write(Tem, 0, Tem.Length)
requestStream.Close()
'read in the page
Dim res As System.Net.HttpWebResponse
res = req.GetResponse()
If req.HaveResponse Then
Dim sr As System.IO.StreamReader
sr = New
System.IO.StreamReader(res.GetResponseStream())
resultString = sr.ReadToEnd
sr.Close()
End If
res.Close()
Please help.
Thanks in advance.
Natalia