File uploading along with form elements using WebRequest or WebClient

Discussion in 'ASP .Net' started by natzol, Dec 1, 2006.

  1. natzol

    natzol Guest

    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
     
    natzol, Dec 1, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mariela

    Uploading files using WebClient

    Mariela, Jan 14, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,983
    Shiv Kumar
    Jan 15, 2004
  2. Andy Johns
    Replies:
    2
    Views:
    403
    Alvin Bruney [MVP]
    Mar 2, 2004
  3. Joerg Jooss
    Replies:
    0
    Views:
    1,130
    Joerg Jooss
    Apr 10, 2004
  4. Aaron

    webrequest webclient

    Aaron, Apr 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,793
    Scott Allen
    Apr 17, 2005
  5. Natalia
    Replies:
    4
    Views:
    784
    Natalia
    Dec 5, 2006
Loading...

Share This Page