Byte Array to Web Service

djohnston

Active member
Joined
Jun 1, 2006
Messages
33
Programming Experience
Beginner
Making this as simple sounding as I can. I already have a form that generates an xml file. I could go 2 ways with it: cycle through the variables and move them as an array through the web service or move the whole entire xml file as a stream through the web service. So now to the part I need help at, I am stuck at actually generating the web service and the code on the form to move the data to the web service via button click. I am just going to be running the Web service vial local host for testing purposes. So if anyone has any kind of sample code or such it would be greatly appreciated. Hoping I can help and learn alot on these forums have a nice day.
 
Further Explanation

I have looked and looked for Web Service Information and have not found what I was looking for. Basically all the examples do basic math and so forth. I need to know how to declare variables that hold values that then can be passed throught a sql connection to a database from the Web Service. As well as How to send the request. Although i am almost positive it is done with a post command.
 
Ok i have the web service created and my file in a byte array i can pass variables to the web service im just trying to figure out how to transfer the whole entire byte array to the web service.
 
Have a read at this article and code samples, it should explain both ends of transferring the bytes of a file between a webservice and a windows/console application. The Code Project "File Server - Web Service" http://www.codeproject.com/vb/net/wsfileserver.asp
 
Start with the most basic testing projects to get going. Your profile is set to .Net 2.0, I don't know what edition you are using, but I will describe a simple example for the free Express editions.

The Visual WebDeveloper Express will let you create a new Web Service project, you can store it locally and it will run in "localhost" environment (as opposed to IIS for internet), it will automatically insert the "Hello World" function method that returns that text as a string. You can easily add a sub method that takes a string type input (or any kind input) and saves this to a file or DB. When running this only at local system permissions is not a problem, but for a public web service on the internet it needs write access to filepath/DB, try it out locally first! If you know how to create a sub method that takes an input parameter you will have this web service with both string input and output up in less than one minute development time! OK, I will be nice and include the code samples, also the auto-generated HelloWorld method:
VB.NET:
[SIZE=2]<WebMethod()> _
[/SIZE][SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] HelloWorld() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return [/COLOR][/SIZE][SIZE=2][COLOR=#800000]"Hello World"
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2]<WebMethod()> _
[/SIZE][SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] submitData([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] input [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Computer.FileSystem.WriteAllText([/SIZE][SIZE=2][COLOR=#800000]"c:\Test.txt"[/COLOR][/SIZE][SIZE=2], input, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
Run the web service (without debugging is ok), description page is now displayed in your browser, first text got a link to the Service Description, copy that link - it is typically something like http:// localhost:2056/WebService1/Service.asmx?WSDL

Now for creating a consumer application. Start Visual Basic 2005 Express edition and create a new Windows Application project. Click main menu, "Project", "Add Web Reference...", the dialog displays where you paste in the address you got above in Url field and click GO. Your local webservice should be listed and you click Add Reference in that same dialog. There is now a new item listed under web references in Solution Explorer, default namespace is "localhost". Here is the simple code to test both methods, the one that gets a text from the web service and the one that inputs a text to the webservice, which in turn will save that text to the local file. First add a TextBox1 to the form, and this code:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] webserv [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] localhost.Service
TextBox1.Text = webserv.HelloWorld()
webserv.submitData([/SIZE][SIZE=2][COLOR=#800000]"submitting input to web service"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
Run the application to see the "Hello World" text display in textbox, afterwards check the local file "c:\Test.txt" to see that the webservice has created it and stored the string inputted.

(edit: added slightly better menu directions.)
 
Last edited:
Update Byte array to variables

Well basically i have been researching for about a day now.

I generated an xml byte array passed it into a webservice now i need to take the byte array and move it back into a set of variables anyone have any ideas maybe even some sample code would be nice.
 
I think your right but

I think your right but I didnt actually serialize to make the byte array Ill add my code so other people can see how i did it and maybe help me figure out how to turn it back into a stream just so i can grab the variables from it.

VB.NET:
Dim buffer As Byte()
Dim fs As FileStream
fs = File.Open(pathxml, FileMode.Open, FileAccess.Read)
Dim lngLen As Long = fs.Length
Dim abyteBuffer(CInt(lngLen - 1)) As Byte
fs.Read(abyteBuffer, 0, CInt(lngLen - 1))
buffer = abyteBuffer

So theres about my third time helping the vb.net community it would be greatly appreciated if someone could help me figure this out.
 
Last edited by a moderator:
So you want to put a byte array into a stream?
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] bts(123) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Byte
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mem [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.MemoryStream
mem.Write(bts, 0, bts.Length)
[/SIZE]

 
xmldatadocument through soap request

I tried to post this under xml but i need vb.net code anyways so.
Basically im trying to figure out how to transfer an xmldatadocument through soap.
Im posting this in vb.net not sure were i should post it at cause it involves ado.net vb.net code and xml.

Any help would be greatly appreciated.
 
I found you had no less than 6 threads going on this issue, so I took the liberty of merging them. Seeing this as a whole also clarifies your different questions a bit on what you actually want to achieve, and possibly we can get a solution soon :)

From the winforms application that got a XmlDocument loaded you can get this as a byte array like this:
VB.NET:
[SIZE=2][COLOR=#008000]'xmldocument to bytes
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] filein [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Application.StartupPath & [/SIZE][SIZE=2][COLOR=#800000]"\tv.xml"
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xdoc [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Xml.XmlDocument
xdoc.Load(filein)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mem [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.MemoryStream
xdoc.Save(mem)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xbytes() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE][SIZE=2] = mem.GetBuffer[/SIZE]
If you take the basic webservice example from post 5 above, there could be a submitXbytes webmethod in your webservice that you call like this to submit these bytes:
VB.NET:
[SIZE=2]webserv.submitXbytes(xbytes)[/SIZE]
'
[SIZE=2]'or directly:[/SIZE]
[SIZE=2][SIZE=2]webserv.submitXbytes(mem.GetBuffer)[/SIZE]
[/SIZE]

The submitXbytes webmethod in your webservice would look like this:
VB.NET:
[SIZE=2]<WebMethod()> _
[/SIZE][SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] submitXbytes([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] xbytes() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]'bytes to xmldocument
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mem [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.MemoryStream
mem.Write(xbytes, 0, xbytes.Length)
mem.Seek(0, IO.SeekOrigin.Begin)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xdoc [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] XmlDocument
xdoc.Load(mem) 
'
[COLOR=#008000]'validating by saving a file copy for review[/COLOR]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fileout [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Server.MapPath([/SIZE][SIZE=2][COLOR=#800000]"App_Data\copy of tv.xml"[/COLOR][/SIZE][SIZE=2])[/SIZE]
xdoc.Save(fileout) 
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
You don't have to save to Xml to a file from the webservice, but just work with the xdoc instance.
Note also that you have to import the System.Xml namespace in the webservice like this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Xml
[/SIZE]
And you don't have to worry about POSTs and SOAP enveloping when communicating with the webservice, .Net framework takes care of all that.
 
Thanks i will jump right on reading and looking into what you are talking about.
Im going to start looking at other posts as well see if i can find some info for people maybe help you out since i see John on alot of posts.
 
Back
Top