如何使用Python的请求将复杂类型发布到WCF?

2024-10-04 01:37:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Python的request包查询wcfweb服务。

我在WCF中创建了一个非常简单的web服务,遵循默认的VS模板:

[ServiceContract]
public interface IHWService
{

    [OperationContract]
    [WebInvoke(Method="GET", UriTemplate="SayHello", ResponseFormat=WebMessageFormat.Json)]
    string SayHello();

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "GetData", ResponseFormat = WebMessageFormat.Json)]
    string GetData(int value);

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "GetData2", BodyStyle=WebMessageBodyStyle.Bare,  RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}

在Python中,我设法调用前两个并轻松地获取数据。在

但是,我尝试调用第三个,它添加了复杂类型的概念。在

这是我的python代码:

^{pr2}$

但是它不起作用,也就是说,如果我在GetDataUsingDataContract方法中对VS进行细分,我会发现composite参数是null。我认为这是由于解析中的一个问题,但我不太清楚是什么问题。在

你看到明显的错误吗?在

你知道我如何在解析机制内调试吗?在

编辑

以下是复杂类型定义:

[DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

Tags: jsonstringvaluepublicmethodvssayhellouritemplate
1条回答
网友
1楼 · 发布于 2024-10-04 01:37:15

您需要在POST body中发送JSON,但您将其附加到查询参数。在

请改用data,只对外部结构进行编码:

result=req.post(wsAddr+methodPath,
                data=json.dumps({'composite': cType}),
                headers=headers)

如果对cType进行编码,则会发送一个JSON编码的字符串,该字符串包含另一个JSON编码的字符串,该字符串又包含您的cType字典。在

相关问题 更多 >