locked
multiform post data using http thingies RRS feed

  • Question

  • User-1914850765 posted

    Strugling to post data to an URL.


    The early supposedly accepts 2 things

    1) profileName

    2) File Data


    Tried This But Get Returned :  ERROR:ProfileName Missing.  Tried so Many variants of different things...


    Dim WebClient As New System.Net.WebClient

    Dim strData As String

            Dim ParamColl As New System.Collections.Specialized.NameValueCollection
            ParamColl.Add("profileName", "fteditorial")


            Dim WebClientResponse As Byte()

            Try
                WebClientResponse = WebClient.UploadValues("http://ftmail.ft.com/interface/list_upload_data.php", "POST",
                MsgBox(System.Text.Encoding.ASCII.GetString(WebClientResponse))
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try


    Uplaod data example below given to me by client.


    POST /interface/list_upload_data.php HTTP/1.1
    Host: response.pure360.com
    User-Agent: www.xyz.com upload process
    Accept: text/plain
    Accept-Language: en
    Accept-Encoding: gzip
    Accept-Charset: ISO-8859-1
    Keep-Alive: 3600
    Connection: keep-alive
    Referer: http://www.xyz.com/uploadRequest.jsp
    Content-Type: multipart/form-data; boundary=---------------------------
    25578952812662351891242608872
    Content-Length: 516
    -----------------------------25578952812662351891242608872
    Content-Dis-data; name="profileName";
    myProfile
    -----------------------------25578952812662351891242608872
    Content-Dis-data; name="123456789"; filename="listData.csv"
    Content-Type: text/plain
    john.smith@email.com,John Smith,07798564352,Brighton
    sarah.jones@email.com,Sarah Jones,0779646352,London
    bob.samuel@email.com,Bob Samuel,0775354542,Cardiff
    -----------------------------25578952812662351891242608872--



    Bloody stumped...

    Tried various HttpRequest object versions I found using streams...


    Help

    Tuesday, July 21, 2009 8:26 AM

Answers

  • User863160722 posted

    The WebClient has methods to upload a collection of values, and methods to upload files, but it doesn't seem to have any methods to upload a combination of the two.

    I managed to work around it by building the request body by hand and using UploadData - the same approach might work for you:

    Using client As New WebClient
    	client.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)
    	
    	Dim boundary As String = "---------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo)
    	client.Headers(HttpRequestHeader.ContentType) = "multipart/form-data; boundary=" + boundary
    	
    	Dim data As Byte() = BuildPostData("--" + boundary) 'NB: The "--" + is important!
    	Dim response As Byte() = client.UploadData("http://ftmail.ft.com/interface/list_upload_data.php", "POST", data)
    	
    	MsgBox(Encoding.ASCII.GetString(response))
    End Using
    
    ...
    
    Function BuildPostData(ByVal boundary As String) As Byte()
    	Using ms As New MemoryStream()
    		Using writer As New StreamWriter(ms, Encoding.UTF8)
    			'Write profileName:
    			writer.WriteLine(boundary)
    			writer.WriteLine("Content-Dis-data; name=""profileName"";")
    			writer.WriteLine()
    			writer.WriteLine("fteditorial")
    			
    			'Write file data:
    			writer.WriteLine(boundary)
    			writer.WriteLine("Content-Dis-data; name=""123456789""; filename=""listData.csv"";")
    			writer.WriteLine("Content-Type: text/plain")
    			writer.WriteLine()
    			writer.WriteLine("john.smith@email.com,John Smith,07798564352,Brighton")
    			writer.WriteLine("sarah.jones@email.com,Sarah Jones,0779646352,London")
    			writer.WriteLine("bob.samuel@email.com,Bob Samuel,0775354542,Cardiff")
    			'TODO: Read the real data from your database or file...
    			
    			writer.WriteLine(boundary + "--")
    			writer.Flush()
    		End  Using
    		
    		Return ms.ToArray()
    	End Using
    End Function

    I've translated this from C#, so it might not be perfect!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2009 3:26 PM

All replies

  • User863160722 posted

    The WebClient has methods to upload a collection of values, and methods to upload files, but it doesn't seem to have any methods to upload a combination of the two.

    I managed to work around it by building the request body by hand and using UploadData - the same approach might work for you:

    Using client As New WebClient
    	client.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)
    	
    	Dim boundary As String = "---------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo)
    	client.Headers(HttpRequestHeader.ContentType) = "multipart/form-data; boundary=" + boundary
    	
    	Dim data As Byte() = BuildPostData("--" + boundary) 'NB: The "--" + is important!
    	Dim response As Byte() = client.UploadData("http://ftmail.ft.com/interface/list_upload_data.php", "POST", data)
    	
    	MsgBox(Encoding.ASCII.GetString(response))
    End Using
    
    ...
    
    Function BuildPostData(ByVal boundary As String) As Byte()
    	Using ms As New MemoryStream()
    		Using writer As New StreamWriter(ms, Encoding.UTF8)
    			'Write profileName:
    			writer.WriteLine(boundary)
    			writer.WriteLine("Content-Dis-data; name=""profileName"";")
    			writer.WriteLine()
    			writer.WriteLine("fteditorial")
    			
    			'Write file data:
    			writer.WriteLine(boundary)
    			writer.WriteLine("Content-Dis-data; name=""123456789""; filename=""listData.csv"";")
    			writer.WriteLine("Content-Type: text/plain")
    			writer.WriteLine()
    			writer.WriteLine("john.smith@email.com,John Smith,07798564352,Brighton")
    			writer.WriteLine("sarah.jones@email.com,Sarah Jones,0779646352,London")
    			writer.WriteLine("bob.samuel@email.com,Bob Samuel,0775354542,Cardiff")
    			'TODO: Read the real data from your database or file...
    			
    			writer.WriteLine(boundary + "--")
    			writer.Flush()
    		End  Using
    		
    		Return ms.ToArray()
    	End Using
    End Function

    I've translated this from C#, so it might not be perfect!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2009 3:26 PM
  • User1010400040 posted

    I've been trying to upload to Pure360 too.  I wrote my own code at first and after many unsuccessful hours of debugging I searched the net and discovered this site.  I battled to get this to work too - I captured my post requests in Fiddler2 and sent it off to Pure360's support several times (they were very helpful). 

    I then discovered that BuildPostData was returning some bogus leading bytes at the beginning of the array.  So I changed the stream writer constructor to ASCII and not UTF8 - lucky guess it now works :-)

    StreamWriter

    writer = new StreamWriter(memStream, Encoding.ASCII);

    I hope this helps someone - it seems a few people have battled with this - including me.

    Regards,

    Dono

    Monday, September 21, 2009 1:41 PM