问题描述
我尝试从与Web服务的通信中获得回报,但是每次发生超时,这段代码中都有错误。
我不知道哪个结构失败了。当我尝试SOAPUI工具时,一切顺利,反馈在1秒钟内到来。但是C#没有响应,它总是导致超时
你能帮我吗?
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);
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)