473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Webclient or HTTPWebrequest post with file and form elements

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=987654 321"
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

Dec 4 '06 #1
4 12681
you need to send a multi-part form. the file should be a mime attachment.

Content-type: multipart/mixed; boundary=23xx1211
CRLF
--23xx1211
Content-type: application/x-www-form-urlencoded
CRLF
a=123&b=345
--23xx1211
Content-type: text/xml
CRLF
<a/a>
--23xx1211--

Natalia wrote:
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=987654 321"
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


Dec 4 '06 #2
I have got :
System.Net.WebException: The Content-Type header cannot be set to a
multipart type for this request.

"bruce barker" <no****@nospam.comwrote in message
news:45**************@nospam.com...
you need to send a multi-part form. the file should be a mime attachment.

Content-type: multipart/mixed; boundary=23xx1211
CRLF
--23xx1211
Content-type: application/x-www-form-urlencoded
CRLF
a=123&b=345
--23xx1211
Content-type: text/xml
CRLF
<a/a>
--23xx1211--

Natalia wrote:
>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(responseArra y).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=987654 321"
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

Dec 4 '06 #3
i don't believe WebClient supports multipart, you have to use the lower
level HttpWebRequest.

-- bruce (sqlwork.com)

Natalia wrote:
I have got :
System.Net.WebException: The Content-Type header cannot be set to a
multipart type for this request.

"bruce barker" <no****@nospam.comwrote in message
news:45**************@nospam.com...
>you need to send a multi-part form. the file should be a mime attachment.

Content-type: multipart/mixed; boundary=23xx1211
CRLF
--23xx1211
Content-type: application/x-www-form-urlencoded
CRLF
a=123&b=345
--23xx1211
Content-type: text/xml
CRLF
<a/a>
--23xx1211--

Natalia wrote:
>>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(responseArr ay).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=987654 321"
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

Dec 4 '06 #4
Ok, I rethink the logic to use HTTPWebRequest as recommended...

Now I am passing parameters in the postURL = "http://site.com?par1=xx&par2=yy".

I do receive the response from the other server back with values, but not file received. What is the problem!?

================================================== =============
Private Function DoWebRequest(ByVal postURL As String, ByVal fileURL As String) As String
Dim boundary As String = Guid.NewGuid().ToString().Replace("-", "")

'DECLARE A FILE===
Dim formdata_Bytes As Byte() = System.Text.Encoding.ASCII.GetBytes("--" & boundary & vbCrLf & "Content-Disposition: form-data; name=\""batch_file\""" & vbCrLf & "content-type: text/plain; charset=windows-1250" & vbCrLf & "content-transfer-encoding:quoted-printable" & vbCrLf & vbCrLf & "VALUE" & vbCrLf & "--" & boundary & "--" & vbCrLf)

'GET FILE===
Dim sF As New FileStream(fileURL, FileMode.Open, FileAccess.Read)
Dim Tem() As Byte
ReDim Tem(sF.Length)
sF.Read(Tem, 0, sF.Length)
sF.Close()

'JOIN FILE DECLARATION AND FILE CONTENTS ===
Dim ByteArrayToSend() As Byte
ReDim ByteArrayToSend(formdata_Bytes.Length + Tem.Length) 'make it big enough
formdata_Bytes.CopyTo(ByteArrayToSend, 0) 'copy Body starting @ 0
Tem.CopyTo(ByteArrayToSend, formdata_Bytes.Length - 1) 'copy File starting @ Body.Length(-1)

'MAKE WEBREQUEST ===
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(postURL), HttpWebRequest)
req.Method = "post"
req.ContentType = "multipart/form-data; boundary=" & boundary
req.ContentLength = ByteArrayToSend.Length

Dim sD As Stream
Try
sD = req.GetRequestStream()
sD.Write(ByteArrayToSend, 0, ByteArrayToSend.Length) 'file content ===
sD.Flush()
Dim res As HttpWebResponse = req.GetResponse()
Dim sr As StreamReader = New StreamReader(res.GetResponseStream())
DoWebRequest = sr.ReadToEnd
sr.Close()
res.Close()

Catch ex As WebException
If ex.Status = WebExceptionStatus.Timeout Then
DoWebRequest = "Timeout error"
Else
DoWebRequest = ex.Message & "<br>"
End If
Finally
sD.Close()
End Try

End Function
"bruce barker" <no****@nospam.comwrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
>i don't believe WebClient supports multipart, you have to use the lower
level HttpWebRequest.

-- bruce (sqlwork.com)

Natalia wrote:
>I have got :
System.Net.WebException: The Content-Type header cannot be set to a
multipart type for this request.

"bruce barker" <no****@nospam.comwrote in message
news:45**************@nospam.com...
>>you need to send a multi-part form. the file should be a mime attachment.

Content-type: multipart/mixed; boundary=23xx1211
CRLF
--23xx1211
Content-type: application/x-www-form-urlencoded
CRLF
a=123&b=345
--23xx1211
Content-type: text/xml
CRLF
<a/a>
--23xx1211--

Natalia wrote:
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(responseAr ray).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=987654 321"
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

Dec 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Dan R Brown | last post by:
I have a large form that is generated dynamically in a jsp using xml / xslt. So, to break up this form into several "tabbed" sections, I break up the form using <div> tags. Each <div...
2
by: jingalls | last post by:
I'm trying to modify a form so that when a user clicks a checkbox for a shorter version of the form, it will replace swap the default (long) form elements with the short version of elements, so...
2
by: Andy Johns | last post by:
I've seen plenty of examples of file uploads in ASP.NET when the only form element is the file input type, but I've not seen a practical example of a file upload routine running alongside other...
17
by: Justin | last post by:
How do I post a form to a specified url using an ASP.NET with a code behind? With traditional ASP I used to be able to simply use: action="https://www.domain.com/process.asp" Thanks, Justin.
25
by: Dave | last post by:
Hello. In trying to get an anchor element to stylistically match an input or button element, I find that the button and input cannot be styled according to the 2.1 CSS spec. For example, I...
0
by: natzol | last post by:
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...
3
by: groups2 | last post by:
I have a form with a dropdown (select) menu a text input field and some hidden values, along with an input botton that triggers an ajax function that submits the form. If the button is after the...
15
by: worked | last post by:
I have a script that hides / shows form elements, and wrapped in a tab script (tab navigation). When the code is duplicated (per tab content), the hide / show function works for the first tab but...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.