如何将字节数组传递给WCF服务

时间:2013-04-04 06:36:59

标签: wcf windows-phone-7

我想从我的Windows Phone应用程序将字节数组传递给WCF服务。网址如下

“HTTP:/serviceurl/Service1.svc/saverecord3 firstarray =”

我该怎么做?

1 个答案:

答案 0 :(得分:0)

这是我的代码的一部分,我用它来发送数据到我的服务器

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http:/serviceurl/Service1.svc/saverecord3");
                req.Method = "POST";

                req.ContentType = "text/xml; charset=utf-8"; //if your data is different, use a content type accordingly
                string postData = @"firstarray=thedatatosend";

                int length = arrayData.Length;

                // Convert the string into a byte array.
                byte[] byteArray = Encoding.UTF8.GetBytes(arrayData);
                req.Headers[HttpRequestHeader.ContentLength] = byteArray.Length.ToString();

                req.BeginGetRequestStream(ar =>
                {
                    using (var requestStream = req.EndGetRequestStream(ar))
                    {
                        // Write the body of your request here
                        requestStream.Write(byteArray, 0, length);
                    }

                    req.BeginGetResponse(a =>
                    {
                        try
                        {
                            var response = req.EndGetResponse(a);
                            var responseStream = response.GetResponseStream();

                            using (var streamRead = new StreamReader(responseStream))
                            {
                                // Get the response message here
                                string responseString = streamRead.ReadToEnd();
                                //Do whatever you want here, with the response
                            }
                        }
                        catch (Exception e)
                        {

                        }

                    }, null);

                }, null);