473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.c om"
Dim q As New System.Collecti ons.Specialized .NameValueColle ction
q.Add("id", "123456789" )
q.Add("merchant _pin", "987654321" )
Dim wc As New System.Net.WebC lient
wc.Headers.Add( "Content-Type",
"applicatio n/x-www-form-urlencoded")
wc.QueryString = q
Dim responseArray As Byte() = wc.UploadFile(u rl, "POST",
"C:\folder\file .txt")
If responseArray.L ength 0 Then
resultString &=
System.Text.Enc oding.ASCII.Get String(response Array).ToString
Else
resultString &= "No response"
End If
=============SE COND- HTTPWebREQUEST =============== ============
RESULT: can pass variables but can not pass the file.
=============== =============== =============== =============== ====
Dim url as String =
"http://www.site2post.c om?id=123456789 &merchant_pin=9 87654321"
Dim strFileToUse As String = "C:\folder\file .txt"
Dim st As New FileStream(strF ileToUse, FileMode.Open)
Dim Tem() As Byte
ReDim Tem(st.Length)
st.Read(Tem, 0, st.Length)
st.Close()
Dim req As System.Net.Http WebRequest
req = System.Net.WebR equest.Create(u rl)
'--set the standard header information
req.ProtocolVer sion = HttpVersion.Ver sion11
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 = "applicatio n/x-www-form-urlencoded"
'req.AllowAutoR edirect = False
req.ContentLeng th = 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.GetRequestS tream()
requestStream.W rite(Tem, 0, Tem.Length)
requestStream.C lose()
'read in the page
Dim res As System.Net.Http WebResponse
res = req.GetResponse ()
If req.HaveRespons e Then
Dim sr As System.IO.Strea mReader
sr = New
System.IO.Strea mReader(res.Get ResponseStream( ))
resultString = sr.ReadToEnd
sr.Close()
End If
res.Close()
Please help.
Thanks in advance.
Natalia

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

Content-type: multipart/mixed; boundary=23xx12 11
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.c om"
Dim q As New System.Collecti ons.Specialized .NameValueColle ction
q.Add("id", "123456789" )
q.Add("merchant _pin", "987654321" )
Dim wc As New System.Net.WebC lient
wc.Headers.Add( "Content-Type",
"applicatio n/x-www-form-urlencoded")
wc.QueryString = q
Dim responseArray As Byte() = wc.UploadFile(u rl, "POST",
"C:\folder\file .txt")
If responseArray.L ength 0 Then
resultString &=
System.Text.Enc oding.ASCII.Get String(response Array).ToString
Else
resultString &= "No response"
End If
=============SE COND- HTTPWebREQUEST =============== ============
RESULT: can pass variables but can not pass the file.
=============== =============== =============== =============== ====
Dim url as String =
"http://www.site2post.c om?id=123456789 &merchant_pin=9 87654321"
Dim strFileToUse As String = "C:\folder\file .txt"
Dim st As New FileStream(strF ileToUse, FileMode.Open)
Dim Tem() As Byte
ReDim Tem(st.Length)
st.Read(Tem, 0, st.Length)
st.Close()
Dim req As System.Net.Http WebRequest
req = System.Net.WebR equest.Create(u rl)
'--set the standard header information
req.ProtocolVer sion = HttpVersion.Ver sion11
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 = "applicatio n/x-www-form-urlencoded"
'req.AllowAutoR edirect = False
req.ContentLeng th = 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.GetRequestS tream()
requestStream.W rite(Tem, 0, Tem.Length)
requestStream.C lose()
'read in the page
Dim res As System.Net.Http WebResponse
res = req.GetResponse ()
If req.HaveRespons e Then
Dim sr As System.IO.Strea mReader
sr = New
System.IO.Strea mReader(res.Get ResponseStream( ))
resultString = sr.ReadToEnd
sr.Close()
End If
res.Close()
Please help.
Thanks in advance.
Natalia


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

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

Content-type: multipart/mixed; boundary=23xx12 11
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.c om"
Dim q As New System.Collecti ons.Specialized .NameValueColle ction
q.Add("id", "123456789" )
q.Add("merchant _pin", "987654321" )
Dim wc As New System.Net.WebC lient
wc.Headers.Add( "Content-Type",
"applicatio n/x-www-form-urlencoded")
wc.QueryString = q
Dim responseArray As Byte() = wc.UploadFile(u rl, "POST",
"C:\folder\fil e.txt")
If responseArray.L ength 0 Then
resultString &=
System.Text.En coding.ASCII.Ge tString(respons eArray).ToStrin g
Else
resultString &= "No response"
End If
=============S ECOND- HTTPWebREQUEST =============== ============
RESULT: can pass variables but can not pass the file.
============== =============== =============== =============== =====
Dim url as String =
"http://www.site2post.c om?id=123456789 &merchant_pin=9 87654321"
Dim strFileToUse As String = "C:\folder\file .txt"
Dim st As New FileStream(strF ileToUse, FileMode.Open)
Dim Tem() As Byte
ReDim Tem(st.Length)
st.Read(Tem, 0, st.Length)
st.Close()
Dim req As System.Net.Http WebRequest
req = System.Net.WebR equest.Create(u rl)
'--set the standard header information
req.ProtocolVer sion = HttpVersion.Ver sion11
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 = "applicatio n/x-www-form-urlencoded"
'req.AllowAutoR edirect = False
req.ContentLeng th = 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.GetRequestS tream()
requestStream.W rite(Tem, 0, Tem.Length)
requestStream.C lose()
'read in the page
Dim res As System.Net.Http WebResponse
res = req.GetResponse ()
If req.HaveRespons e Then
Dim sr As System.IO.Strea mReader
sr = New
System.IO.Stre amReader(res.Ge tResponseStream ())
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.WebE xception: The Content-Type header cannot be set to a
multipart type for this request.

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

Content-type: multipart/mixed; boundary=23xx12 11
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.c om"
Dim q As New System.Collecti ons.Specialized .NameValueColle ction
q.Add("id", "123456789" )
q.Add("merchant _pin", "987654321" )
Dim wc As New System.Net.WebC lient
wc.Headers.Add( "Content-Type",
"applicatio n/x-www-form-urlencoded")
wc.QueryString = q
Dim responseArray As Byte() = wc.UploadFile(u rl, "POST",
"C:\folder\fi le.txt")
If responseArray.L ength 0 Then
resultString &=
System.Text.E ncoding.ASCII.G etString(respon seArray).ToStri ng
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.c om?id=123456789 &merchant_pin=9 87654321"
Dim strFileToUse As String = "C:\folder\file .txt"
Dim st As New FileStream(strF ileToUse, FileMode.Open)
Dim Tem() As Byte
ReDim Tem(st.Length)
st.Read(Tem, 0, st.Length)
st.Close()
Dim req As System.Net.Http WebRequest
req = System.Net.WebR equest.Create(u rl)
'--set the standard header information
req.ProtocolVer sion = HttpVersion.Ver sion11
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 = "applicatio n/x-www-form-urlencoded"
'req.AllowAutoR edirect = False
req.ContentLeng th = 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.GetRequestS tream()
requestStream.W rite(Tem, 0, Tem.Length)
requestStream.C lose()
'read in the page
Dim res As System.Net.Http WebResponse
res = req.GetResponse ()
If req.HaveRespons e Then
Dim sr As System.IO.Strea mReader
sr = New
System.IO.Str eamReader(res.G etResponseStrea m())
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=x x&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(By Val postURL As String, ByVal fileURL As String) As String
Dim boundary As String = Guid.NewGuid(). ToString().Repl ace("-", "")

'DECLARE A FILE===
Dim formdata_Bytes As Byte() = System.Text.Enc oding.ASCII.Get Bytes("--" & boundary & vbCrLf & "Content-Disposition: form-data; name=\""batch_f ile\""" & 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(file URL, 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(ByteArra yToSend, 0) 'copy Body starting @ 0
Tem.CopyTo(Byte ArrayToSend, formdata_Bytes. Length - 1) 'copy File starting @ Body.Length(-1)

'MAKE WEBREQUEST ===
Dim req As HttpWebRequest = DirectCast(WebR equest.Create(p ostURL), HttpWebRequest)
req.Method = "post"
req.ContentType = "multipart/form-data; boundary=" & boundary
req.ContentLeng th = ByteArrayToSend .Length

Dim sD As Stream
Try
sD = req.GetRequestS tream()
sD.Write(ByteAr rayToSend, 0, ByteArrayToSend .Length) 'file content ===
sD.Flush()
Dim res As HttpWebResponse = req.GetResponse ()
Dim sr As StreamReader = New StreamReader(re s.GetResponseSt ream())
DoWebRequest = sr.ReadToEnd
sr.Close()
res.Close()

Catch ex As WebException
If ex.Status = WebExceptionSta tus.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******** ********@TK2MSF TNGP02.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.Web Exception: 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=23xx12 11
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.c om"
Dim q As New System.Collecti ons.Specialized .NameValueColle ction
q.Add("id", "123456789" )
q.Add("merchant _pin", "987654321" )
Dim wc As New System.Net.WebC lient
wc.Headers.Add( "Content-Type",
"applicati on/x-www-form-urlencoded")
wc.QueryString = q
Dim responseArray As Byte() = wc.UploadFile(u rl, "POST",
"C:\folder\f ile.txt")
If responseArray.L ength 0 Then
resultString &=
System.Text. Encoding.ASCII. GetString(respo nseArray).ToStr ing
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.c om?id=123456789 &merchant_pin=9 87654321"
Dim strFileToUse As String = "C:\folder\file .txt"
Dim st As New FileStream(strF ileToUse, FileMode.Open)
Dim Tem() As Byte
ReDim Tem(st.Length)
st.Read(Tem, 0, st.Length)
st.Close()
Dim req As System.Net.Http WebRequest
req = System.Net.WebR equest.Create(u rl)
'--set the standard header information
req.ProtocolVer sion = HttpVersion.Ver sion11
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 = "applicatio n/x-www-form-urlencoded"
'req.AllowAutoR edirect = False
req.ContentLeng th = 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.GetRequestS tream()
requestStream.W rite(Tem, 0, Tem.Length)
requestStream.C lose()
'read in the page
Dim res As System.Net.Http WebResponse
res = req.GetResponse ()
If req.HaveRespons e Then
Dim sr As System.IO.Strea mReader
sr = New
System.IO.St reamReader(res. GetResponseStre am())
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
40658
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 style="display:none"> can be displayed by setting the style attribute to "display:", or hidden with "display:none". This gives the illusion that the person...
2
1870
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 that only the version that is selected will have its element values passed on to the next page. Currently I have two div areas set up with ids as...
2
1899
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 form elements, e.g. a text box for a description for example. If the form is being posted in multipart format then the text coming through will need...
17
1635
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
2455
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 expected this ruleset: ..but { display: block; background-color: red; font-size: 14px; text-decoration: none; line-height: 16px; width: auto; height:...
0
1460
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 advices but still having troubles... it might some obvious error that I am making but I just dont see it. ==================FIRST -...
3
1538
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 select statement, the chosen value of the selection of the dropdown is posted. But if the button is before the select statement, everything in the...
15
3789
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 breaks in each subsequent tab. Why? Help appreciated! Example: http://geocities.com/edmurphy21/ hide / show script: <script...
0
7704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7620
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7931
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7684
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3666
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.