使用HTTP请求使用WebService [英] Consume WebService Using HTTP Request

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

问题描述

如何使用带有XML Soap信封文件的HTTP请求对象来使用Web wervice.
谢谢

How can I consume web wervices using HTTP request object with XML Soap envolpe file.
Thanks

推荐答案

您的问题尚不清楚,但是下面的代码可以帮助您开始...

如果这是您的休息服务:

Your question is not clear, but here is some code which could help you starting...

If this is your rest service:

[DataContract]
[Serializable]
public class SendData
{
    [DataMember(Order=1)]
    public string cmd;

    [DataMember(Order=2)]
    public string data;
}

namespace My_Server
{
    [ServiceContract]
    public interface IRestService
    {
        [WebInvoke(Method = "POST", 
		UriTemplate = "logon", 
		RequestFormat = WebMessageFormat.Xml, 
		ResponseFormat = WebMessageFormat.Xml, 
		BodyStyle = WebMessageBodyStyle.Bare)]
        [OperationContract]
        LogonResult DoLogon(SendData data);
    }
}



...然后您的客户端cmd可能如下所示:



...then your client cmd could look like this:

private void DoLogon(string credentials)
{
    SendData sd = new SendData();
    sd.cmd = "EXTERNAL-USER";
    sd.data = credentials;
    XmlDocument xdoc = HttpPost("https://yourserver-ip:443/internal/logon", sd);
    // convert xml document from the server to object of type LogonResult
    DataContractSerializer dcSer = new DataContractSerializer(typeof(LogonResult));
    StringWriter stringWriter = new StringWriter();
    XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
    xdoc.WriteTo(xmlWriter);
    MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(stringWriter.ToString()));
    stream.Position = 0;
    XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
    LogonResult res = (LogonResult)dcSer.ReadObject(reader, true);
            
do something with the result...    
}



服务器和客户端之间共享的类库:



The class library to share between server and client:

namespace DataLib
{
    [Serializable]
    public class LogonResult
    {
        public bool bCmdSuccess;
        public string sInfo;
    }
}


这篇关于使用HTTP请求使用WebService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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