File uploading along with form elements using WebRequest or WebClient

N

natzol

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top