简体   繁体   English

在WCF中处理来自XML HTTP的POST请求

[英]Handle POST request from XML HTTP in WCF

Hi and good day everyone, 大家好,大家好

as per above title, I was trying to handle POST request from XML HTTP in WCF. 按照上面的标题,我试图处理WCF中来自XML HTTP的POST请求。 Actually, I can already handle the request by using HTTP Web Request, but since old library sends the request in this form (as in the code, we are using MSXML2 namespace), we will have to maintain the requirement here. 实际上,我已经可以通过使用HTTP Web Request来处理请求,但是由于旧库以这种形式发送请求(如代码中一样,我们使用的是MSXML2命名空间),因此我们必须在此保留要求。

Here are the codes for the front-end (the old library that will send the request) 这是前端的代码(将发送请求的旧库)

MSXML2.ServerXMLHTTP xmlhttp = new MSXML2.ServerXMLHTTP();                 
xmlhttp.open("POST", "http://localhost/RestService/RestServiceImpl.svc/auth", false);
xmlhttp.setRequestHeader("User-Agent", "Jakarta Commons-HttpClient/2.0.2");
String ClientRequest = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone= ""yes""?>   <RequestData xmlns=""http://www.eysnap.com/mPlayer""> <details>Ashu|29|7 Years|.NET</details></RequestData>";

xmlhttp.send(ClientRequest);
int readyState = xmlhttp.readyState;
int status = xmlhttp.status;

While here are the codes for the WCF, which I failed to handle it: 虽然这里是WCF的代码,但我无法处理它:

public ResponseData Auth(RequestData rData)        
    {
        try
        {
            Logger.log.Info("attempting to send request");
            // Call BLL here
            var data = rData.details.Split('|');

            var response = new ResponseData                
            {
                Name = data[0],
                Age = data[1],
                Exp = data[2],
                Technology = data[3]
            };

            Logger.log.Info("Sending request...");
            return response;
        }

        catch(System.Exception ex)
        {
            Logger.log.Fatal(ex.Message);
            return null;
        }

    }

In the web.config, I already change the binding to "customBinding" instead of "webHttpBinding" because I was getting error on RAW format instead of JSON format for the request. 在web.config中,我已经将绑定更改为“ customBinding”而不是“ webHttpBinding”,因为我在请求的RAW格式而不是JSON格式上遇到错误。

After searching through the forum and other sites, I tried on changing the web.config accordingly but got xmlhttp status 400 (Bad Request) instead. 在论坛和其他站点中搜索之后,我尝试相应地更改web.config,但是却改为xmlhttp status 400(错误请求)。

Thanks in advance for the advices. 在此先感谢您的建议。

Try to monitor your request using Fiddler and see how the RAW request looks like. 尝试使用Fiddler监视您的请求,并查看RAW请求的外观。 It should be something like below: 它应该像下面这样:

POST http://localhost/RestService/RestServiceImpl.svc/auth HTTP/2.0
Content-Type: application/xml
Content-Length: 47

<RequestData xmlns="http://www.eysnap.com/mPlayer"xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><details>Ashu|29|7 Years|.NET</details></RequestData>

thanks for the reply. 谢谢回复。 I changed Content-Type header: 我更改了Content-Type标头:

xmlhttp.setRequestHeader("Content-Type", "application/xml");

It works! 有用! I now can change the xml input to the WCF and get the status 200. 现在,我可以将XML输入更改为WCF并获取状态200。

However, for the monitoring using Fiddler, I did open up Fiddler, but I am not sure if I got this right: 但是,对于使用Fiddler进行监视,我确实打开了Fiddler,但是不确定是否正确:

POST http://localhost:3015/default.aspx HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://localhost:3015/default.aspx
Accept-Language: en-MY
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:3015
Content-Length: 220
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=qdzso1frp32nzw1yyca0xkev

    __VIEWSTATE=%2FwEPDwUKLTUwNDQwMTA1MGRk2SgUm9rav%2BZYiqZHo5PtLZx1Eh67%2BxU309q4TRd3NLU%3D&    __EVENTVALIDATION=%2FwEWBAKr8%2BqJAQLj5PzKCwKM54rGBgLs0bLrBr7W%2BQ74Ywyf9FnYR2DQIpRMPmXllwC5V5bOZfXnofpY&Button1=XML+HTTP&TextBox1=

Thus, I did not get the expected result like in your last post. 因此,我没有像您上一篇文章那样获得预期的结果。 Is there any additional setting that I should do first before opening up my sample web application? 打开示例Web应用程序之前,我应该首先做其他设置吗?

By the way, how can I remove namespace from Data Contract? 顺便说一句,如何从数据协定中删除名称空间?

<RequestData xmlns=""http://www.eysnap.com/mPlayer""><details>Ashu|29|7 Years|.NET</details></RequestData>

is the old request, I tried to change to: 是旧的请求,我尝试更改为:

<RequestData><details>Ashu|29|7 Years|.NET</details></RequestData>

and in RequestData class as well: 以及在RequestData类中:

[DataContract()]    
public class RequestData
{
    [DataMember]        
    public string details { get; set; }
}

this is because actually existing request does not have namespace. 这是因为实际上现有的请求没有名称空间。 Now I am getting error 400 for removing the namespace. 现在,我收到删除命名空间的错误400。 Would it be possible to remove namespace in first place? 首先可以删除名称空间吗?

Thanks 谢谢

Edited: I change DataContract to: 编辑:我将DataContract更改为:

[DataContract(Namespace="")]    
public class RequestData
{
    [DataMember]        
    public string details { get; set; }
}

and managed to get HTTP status to 200! 并成功将HTTP状态设置为200! Now I am trying to change the request xml to the one that our existing application use. 现在,我尝试将请求xml更改为我们现有应用程序使用的XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM