HttpWebRequest POST of base64 xml data

Posted on 2006-04-27
Last Modified: 2008-02-01
I am trying to figure out how to post xml that has been base64 encoded to a server using System.Net.HttpWebRequest.  The base64 encoded string needs to appended to a parameter so that the receiving server can access the value.

I am having issues because of the fact that I need to encode the base64 encoded string so that I can write it to a stream.  Here is my code:

******************** CODE****************************************
dim sBody as string = [xml string that has been generated]
dim action as string = [url]

Dim data As Byte() = (New System.Text.UTF8Encoding).GetBytes(sBody)
Dim strEncoded As String = Convert.ToBase64String(data)

Dim postData As String = "param=" + strEncoded
Dim byteData As Byte() = (New System.Text.ASCIIEncoding).GetBytes(postData)

Dim myRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(action)
myRequest.Method = "POST"
myRequest.ContentType = "application/x-www-form-urlencoded"
myRequest.ContentLength = byteData.Length

Dim newStream As System.IO.Stream = myRequest.GetRequestStream
newStream.Write(byteData, 0, byteData.Length)

Dim myResponse As System.Net.HttpWebResponse = myRequest.GetResponse

********************END  CODE************************************

I think the issue occurs when I encode the value that needs to be posted.  I have tried several different encoding types and none of them have been successful.  I know that using the ASCIIEncoding will turn any plus signs (+) into spaces and that is another issue.

If anyone knows of a way to do this, even using another method let me know.  One requirement is that after the post I do not want to navigate the user to the other server.  I just want to post the data and be able to view the response from the server and move on with functionality on my site.  Is there a way to accomplish this post without converting the string to a byte array?  

Thank you in advance.
Question by:asfilo
    LVL 23

    Expert Comment


    Author Comment

    I don't think the article helps me at all.  I still think the issue is that I need to base64 encode my xml string and then use the encoding class to convert it back to a byte array.  I think there are some issues around that conversion from string to byte array that may be changing some of the characters in my xml string (for instance, it has been verified that using a few of the encoding types that any plus signs that are in the xml string get converted to spaces).

    Is that a possibility or am I looking for issues where there aren't any?
    LVL 23

    Accepted Solution

    You have to URL encode the XML string otherwise you may end  up loosing some data in tranmsission because of certain characters like spaces etc.

    Author Comment

    I ended up having to URL encrpt the base64 representation of the xml string.  Thanks for all of your help.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now