使用 HttpWebRequest 返回原始肥皂体
我尝试将信息发送到服务并使用此代码返回原始肥皂体。是否可以?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这样是不可能的。您正在发送 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.