使用HttpWebRequest返回原始肥皂体

时间:2011-09-21 11:41:02

标签: wcf soap httpwebrequest

我尝试将信息发送到服务并使用此代码返回原始soap body。有可能吗?

  DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GetInfoRequest));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/Service.svc/soap/GetDataSoap");
            GetInfoRequest message = new GetInfoRequest();
            message.data = new List<int>();
            message.data.Add(268435458);
            message.data.Add(99);

            MemoryStream stream1 = new MemoryStream();
            serializer.WriteObject(stream1, message);
            stream1.Position = 0;
            StreamReader sr = new StreamReader(stream1);
            string t = sr.ReadToEnd();

            ASCIIEncoding encoding = new ASCIIEncoding();
            request.Timeout = 99999999;
            request.ContentLength = t.Length;
            //request.ContentType = "application/json";
            request.Method = "POST";
            request.Headers.Add("SOAPAction: \"http://localhost/Service.svc/soap/GetDataSoap\"");
            request.Accept = "text/xml; charset=utf-8";
            request.ContentType = "application/json; charset=utf-8";


            using (Stream requestStream = request.GetRequestStream())
            {
                var bytes = Encoding.UTF8.GetBytes(t);
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                //serializer.WriteObject(stream1, message);
                //requestStream.Flush();
            }

            var response = (HttpWebResponse)request.GetResponse();
            var abc = new StreamReader(response.GetResponseStream()).ReadToEnd();
            TextBox1.Text = abc;

1 个答案:

答案 0 :(得分:0)

不可能这样。您正在发送JSON请求并期望SOAP响应。那样不行。您必须向SOAP服务发送有效的SOAP请求并获得SOAP响应。