发送肥皂请求c# [英] send soap request c#

查看:105
本文介绍了发送肥皂请求c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我希望有人可以提供帮助。我使用此代码不断收到内部服务器错误500。我开始疯了。



这是代码:



  public   static   void  getAuthenticationToken() 
{
StringBuilder xml = new StringBuilder();
xml.Append( @ <?xml version =1.0encoding =utf -8 >?);
xml.Append( @ < soapenv:Envelope xmlns:soapenv =http:// schemas .xmlsoap.org / soap / envelope /);
xml.Append( @ xmlns:ser =http://www.UKMail.com/服务/合同/ ServiceContracts);
xml.Append( @ xmlns:dat =http://www.UKMail.com/服务/合同/ DataContracts >中);
xml.Append( < soapenv:Header />);
xml.Append( < soapenv:Body>);
xml.Append( < ser:Login>);
xml.Append( < ser:loginWebRequest>);
xml.Append( < dat:Password> password< / dat:Password>);
xml.Append( < dat:Username> username< / dat:Username>);
xml.Append( < / ser:loginWebRequest>);
xml.Append( < / ser:Login>);
xml.Append( < / soapenv:Body>);
xml.Append( < / soapenv:Envelope>);

string s = getUKMailData(xml.ToString(), https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl);
}

public static void getUKMailData( string xml, string 地址)
{
string result = ;
HttpWebRequest request = CreateWebRequest(address);
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(xml);

使用(Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}

使用(WebResponse response = request.GetResponse()) // 此处出错
{
使用(StreamReader rd = < span class =code-keyword> new
StreamReader(response.GetResponseStream()))
{
string soapResult = rd .ReadToEnd();
Console.WriteLine(soapResult);
}
}
}

public static HttpWebRequest CreateWebRequest( string url)
{
HttpWebRequest webRequest =(HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add( SOAP:Action);
webRequest.ContentType = text / xml; charset = \utf-8 \< /跨度>;
webRequest.Accept = text / xml;
webRequest.Method = POST;
return webRequest;
}







我是SOAP的新手,我不知道怎么做让它在.NET中工作。任何帮助都将不胜感激。

解决方案

令人惊讶的是,您正在尝试手动编写XML文本,甚至不使用.NET中提供的任何XML类型。为什么不使用为此目的设计的.NET FCL类型?请参阅:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter%28v=vs.110%29.aspx [ ^ ]。



我不知道你是否也在开发这项服务。如果这样做,使用更高级别的抽象重新定义它更合适,例如数据合同服务合同。这将是一个完全不同的故事。现在,我建议从.NET BCL开始阅读标准库为您提供的内容。



-SA

您可能已经解决了请求的问题,但为什么所有这些都有效?您只需通过提供WCF网址即可为项目添加服务引用。然后它将为服务生成客户端代理类。然后你可以调用下面的方法



 UKMAuthenticationServiceClient client =  new  UKMAuthenticationServiceClient(); 
var response = client.Login( new LoginWebRequest(){Password = ??,用户名= ??});





如何:添加,更新或删除服务参考 [ ^ ]


问题是

 webRequest.Headers.Add(  SOAP:行动); 





我改为:



 webRequest.Headers.Add(  SOAPAction,行动); 





action =http://www.UKMail的.com /服务/ IUKMAuthenticationService /登录;


Hi all,

I hope someone can help. I keep getting 'Internal Server Error 500' with this code. I'm starting to go insane.

Here is code:

public static void getAuthenticationToken()
{
  StringBuilder xml = new StringBuilder();
  xml.Append(@"<?xml version=""1.0"" encoding=""utf-8""?>");
  xml.Append(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" ");
  xml.Append(@"xmlns:ser=""http://www.UKMail.com/Services/Contracts/ServiceContracts"" ");
  xml.Append(@"xmlns:dat=""http://www.UKMail.com/Services/Contracts/DataContracts"">");
  xml.Append("<soapenv:Header/>");
  xml.Append("<soapenv:Body>");
  xml.Append("<ser:Login>");
  xml.Append("<ser:loginWebRequest>");
  xml.Append("<dat:Password>password</dat:Password>");
  xml.Append("<dat:Username>username</dat:Username>");
  xml.Append("</ser:loginWebRequest>");
  xml.Append("</ser:Login>");
  xml.Append("</soapenv:Body>");
  xml.Append("</soapenv:Envelope>");

  string s = getUKMailData(xml.ToString(), "https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl");
}

public static void getUKMailData(string xml, string address)
{           
   string result = "";
   HttpWebRequest request = CreateWebRequest(address);
   XmlDocument soapEnvelopeXml = new XmlDocument();
   soapEnvelopeXml.LoadXml(xml);

   using (Stream stream = request.GetRequestStream())
   {
      soapEnvelopeXml.Save(stream);
   }

   using (WebResponse response = request.GetResponse()) // Error occurs here
   {
      using (StreamReader rd = new StreamReader(response.GetResponseStream()))
      {
         string soapResult = rd.ReadToEnd();
         Console.WriteLine(soapResult);
      }
   }
}

public static HttpWebRequest CreateWebRequest(string url)
{
   HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
   webRequest.Headers.Add("SOAP:Action");
   webRequest.ContentType = "text/xml;charset=\"utf-8\"";
   webRequest.Accept = "text/xml"; 
   webRequest.Method = "POST";
   return webRequest;
}




I'm new to SOAP and I'm not sure how to get it to work in .NET. Any help would be appreciated.

解决方案

It's amazing to see that you are trying to manually compose XML text, not even using any of the XML types available in .NET. Why not using .NET FCL types designed for such purposes? Please see:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter%28v=vs.110%29.aspx[^].

And I don't know if you are developing the service itself as well. If you do, it would be more appropriate to redefine it using much higher levels of abstractions, such as Data Contract or Service Contract. This would be a whole different story. For now, I would advise to read what the standard libraries offer for you, starting from .NET BCL.

—SA


you may have already fix the issue with the request, but why all these work? you can simply add service reference to your project by giving the WCF url. then it will generate client proxy class for the service. then you can call the methods like below

UKMAuthenticationServiceClient client = new UKMAuthenticationServiceClient();
var response  = client.Login(new LoginWebRequest() { Password = "??", Username = "??" });



How to: Add, Update, or Remove a Service Reference[^]


The problem was

webRequest.Headers.Add("SOAP:Action");



I changed it to:

webRequest.Headers.Add("SOAPAction", action);



action = "http://www.UKMail.com/Services/IUKMAuthenticationService/Login";


这篇关于发送肥皂请求c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆