[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


HttpWebRequest POST of base64 xml data

Posted on 2006-04-27
Medium Priority
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
  • 2
  • 2
LVL 23

Expert Comment

ID: 16557492

Author Comment

ID: 16557808
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

naveenkohli earned 2000 total points
ID: 16557854
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

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month19 days, 12 hours left to enroll

873 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