如何传递Soap Request asp.net,C#的值 [英] How to pass values for soap Request asp.net,c#

查看:116
本文介绍了如何传递Soap Request asp.net,C#的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络服务,我将URL作为网络参考信息添加到了我需要发送请求的其他网站上,我正在查看google,但这对我来说有点困难,因为我对此并不陌生,我我想在这里做的是一个按钮单击事件,我必须使用文本框中的参数调用存储过程,结果应该是xml格式,我在代码的某些部分遇到麻烦,例如传递参数和东西可以帮助我关于如何使用它,以下是我正在使用的代码,如果有任何错误,请让我知道,在此先感谢


HI i have a web service and i added the url as web refrence to other website from where i need to send request, i am looking over google but it is kind of hard for me as i was new to this , what i am trying to do here is on a button click event i have to invoke the store procedure with parameters from textboxes and the result should be in xml format , i am having trouble on some parts of code like passing parametrs and stuff can some help me out on how to acieve it , the below is the code i am working on , please let me know if its is having any errors, thanks in advance


private static string webservicecall(string getbrokerdetails)
    {

        WebRequest req = WebRequest.Create("http://localhost/SOAPREQUESTRESPONSE/Service.asmx");
        HttpWebRequest httpreq = (HttpWebRequest)req;
        httpreq.Method = "POST";
        httpreq.ContentType = "text/xml; charset=utf-8";
        httpreq.Headers.Add("SOAPAction:http://tempuri.org/getbrokerdetails");
        httpreq.ProtocolVersion=HttpVersion.Version11;
        httpreq.Credentials=CredentialCache.DefaultCredentials;
        Stream  str=httpreq.GetRequestStream();
        StreamWriter strwriter=new StreamWriter(str,Encoding.ASCII);
        StringBuilder soaprequest=new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        soaprequest.Append ("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
        soaprequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
        soaprequest.Append("<GetMyName xmlns=\"http://tempuri.org/\"><name>Sam</name></GetMyName>");
        soaprequest.Append("<compcode>blgf</compcode>");
        soaprequest.Append("<period>20111st</period>");
        soaprequest.Append("</soap:Body></soap:Envelope>");

       strwriter.Write(soaprequest.ToString());
       strwriter.Close();
        HttpWebResponse res=(HttpWebResponse)httpreq.GetResponse();
        StreamReader rdr=new StreamReader(res.GetResponseStream());
        string result=rdr.ReadToEnd();
        return result;



    }

推荐答案

像这样手动滚动自己的请求并不常见(而且很困难!),通常是通过工具生成代理客户端.

您可以(明确地说,如果您有webservice项目可用)右键单击 client 项目并添加Web引用,然后将向导指向您要使用的服务,这将生成客户端.

如果未公开该服务,则您可以针对该服务运行名为wsdl.exe的工具.在此处有一些帮助 [消费数据网络asmx服务 [^ ] ,您只需要忽略WPF内容即可.您的示例代码看起来像

It is not usual (and hardish!) to hand-roll your own reqest like this, normally you generate a proxy client via a tool.

You can probably (definately if you have the webservice project available to you) right click the client project and add a Web Reference, then point the wizard at the service you want to consume, this generated the client.

Failing that you can run a tool called wsdl.exe against the service, assuming it has a wsdl exposed. There is some help here[^].

Once you have the client calling the web method is as simple as creating an instance of the generated proxy client, calling the method on the instance then closing the proxy.

This is the best article I could find :consuming data web asmx service[^], you just have to ignore the WPF stuff. The code for your example woul look like

using (FooServiceClient proxy = new FooServiceClient())
{
  string result = proxy.GetMyName("sam");
}



其中FooServiceClient是客户端生成的.



Where FooServiceClient is the client generated.


如果您有第三方提供的wsdl,则可以从中生成类对象和函数,并进行Web服务调用...(由回答基思)

尽管我有时会使用SoapUI工具来测试我的肥皂请求.一旦成功,我将复制该请求并将其另存为xml文件.然后,我读取了该xml文件,并将值放在需要的标记中,并将其发布为http Web请求请求.您也可以根据需要使用证书对其进行签名..它有效.
但是我知道上面的方法不是一个好方法,可以说是很麻烦.
if you have wsdls given from a 3rd party you can generate class objects and functions from them and make web services calls... (as answered by keith)

though I sometimes use SoapUI tool to test my soap request. once successful i copy that request and save it as xml file. then i read that xml file and put values in tags where required and post it as http web request request. you can also sign it using certificate if required.. it works.
however i know the approach above is not a good one and can be said as a heck.


我认为您的标头出了点问题:

I think something wrong with your header :

httpreq.Headers.Add("SOAPAction:http://tempuri.org/getbrokerdetails");



据我了解,您没有提供正确的URL操作名称,因为您的服务方法指向Localhost,因此请尝试以下操作:



As per my understanding, you are not providing correct URL Action name because your service method pointing Localhost so try this:

httpreq.webRequest.Headers.Add("SOAPAction", "http://localhost/SOAPREQUESTRESPONSE/WebMethodName");



确保请求格式正确,因此调试代码,如果请求字符串不起作用,请在此处发布请求字符串.

参考网址:

http://mikehadlow.blogspot.com/2006/05/making- raw-web-service-calls-with.html [ ^ ]



Make sure request format is correct so debug the code and post here request string if it not working.

Ref. URL:

http://mikehadlow.blogspot.com/2006/05/making-raw-web-service-calls-with.html[^]


这篇关于如何传递Soap Request asp.net,C#的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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