使用 HttpWebRequest 返回原始肥皂体

发布于 2024-12-06 06:20:15 字数 1710 浏览 0 评论 0原文

我尝试将信息发送到服务并使用此代码返回原始肥皂体。是否可以?

  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;

I tried to send information to a service and return the raw soap body using this code. Is it possible?

  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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坠似风落 2024-12-13 06:20:15

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

No it is not possible this way. You are sending JSON request and expect SOAP response. That will not work. You must send valid SOAP request to your SOAP service and get SOAP response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文