C# 如何从Soap WSDL获取响应(超时)
我试图从与web服务的通信中获取返回,但每次都出现代码错误 我不知道哪个结构在失效。当我尝试SOAPUI工具时,一切都进行得很顺利,反馈在1秒内就出现了。但是C#没有响应,它总是导致超时 你能帮我吗C# 如何从Soap WSDL获取响应(超时),c#,soap,wsdl,response,webrequest,C#,Soap,Wsdl,Response,Webrequest,我试图从与web服务的通信中获取返回,但每次都出现代码错误 我不知道哪个结构在失效。当我尝试SOAPUI工具时,一切都进行得很顺利,反馈在1秒内就出现了。但是C#没有响应,它总是导致超时 你能帮我吗 using System.Xml; using System.Net; using System.IO; using System; namespace ProjetosSoap { class Program { static void Main(strin
using System.Xml;
using System.Net;
using System.IO;
using System;
namespace ProjetosSoap
{
class Program
{
static void Main(string[] args)
{
CallWebService();
}
public static void CallWebService()
{
var _url = "https://www.cnj.jus.br/sgt/sgt_ws.php?wsdl";
var _action = "https://www.cnj.jus.br/sgt/sgt_ws.php#pesquisarItemPublicoWS";
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "gzip,deflate";
webRequest.Method = "POST";
webRequest.ContentLength = 404;
webRequest.Host = "www.cnj.jus.br";
webRequest.KeepAlive = true;
webRequest.UserAgent = "Apache-HttpClient/4.1.1 (java 1.5)";
webRequest.Timeout = 32000;
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(
@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:sgt=""https://www.cnj.jus.br/sgt/sgt_ws.php"">
<soapenv:Header/>
<soapenv:Body>
<sgt:pesquisarItemPublicoWS>
<tipoTabela>M</tipoTabela>
<tipoPesquisa>C</tipoPesquisa>
<valorPesquisa>335</valorPesquisa>
</sgt:pesquisarItemPublicoWS>
</soapenv:Body>
</soapenv:Envelope>");
return soapEnvelopeDocument;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
}
}
使用System.Xml;
Net系统;
使用System.IO;
使用制度;
名称空间项目
{
班级计划
{
静态void Main(字符串[]参数)
{
CallWebService();
}
公共静态void CallWebService()
{
var_url=”https://www.cnj.jus.br/sgt/sgt_ws.php?wsdl";
var_action=”https://www.cnj.jus.br/sgt/sgt_ws.php#pesquisarItemPublicoWS";
XmlDocument soapEnvelopeXml=CreateSoapEnvelope();
HttpWebRequest webRequest=CreateWebRequest(_url,_action);
插入SOAPEnvelopeInToWebRequest(SOAPEnvelopeExML,webRequest);
//开始对web请求的异步调用。
IAsyncResult asyncResult=webRequest.BeginGetResponse(null,null);
//挂起此线程,直到调用完成。您可能希望
//在这里做一些有用的事情,比如更新你的UI。
asyncResult.AsyncWaitHandle.WaitOne();
//从已完成的web请求中获取响应。
字符串soapResult;
使用(WebResponse WebResponse=webRequest.EndGetResponse(asyncResult))
{
使用(StreamReader rd=newstreamreader(webResponse.GetResponseStream()))
{
soapResult=rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
私有静态HttpWebRequest CreateWebRequest(字符串url,字符串操作)
{
HttpWebRequest webRequest=(HttpWebRequest)webRequest.Create(url);
webRequest.Headers.Add(“SOAPAction”,action);
webRequest.ContentType=“text/xml;charset=\”utf-8\”;
webRequest.Accept=“gzip,deflate”;
webRequest.Method=“POST”;
webRequest.ContentLength=404;
webRequest.Host=“www.cnj.jus.br”;
webRequest.KeepAlive=true;
webRequest.UserAgent=“Apache HttpClient/4.1.1(java 1.5)”;
webRequest.Timeout=32000;
返回webRequest;
}
私有静态XmlDocument CreateSOApevelope()
{
XmlDocument SOAPEnvelopedDocument=新的XmlDocument();
soapEnvelopedDocument.LoadXml(
@"
M
C
335
");
返回SOAPEnveloped文档;
}
私有静态void InsertSoapEnvelopeIntoWebRequest(XmlDocument SoapEnvelopeExML,HttpWebRequest webRequest)
{
使用(Stream-Stream=webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(流);
}
}
}
}