在 WCF 中处理来自 XML HTTP 的 POST 请求 [英] Handle POST request from XML HTTP in WCF

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

问题描述

大家好,

根据上面的标题,我试图在 WCF 中处理来自 XML HTTP 的 POST 请求.实际上,我已经可以使用 HTTP Web 请求来处理请求,但是由于旧库以这种形式发送请求(如代码中,我们使用的是 MSXML2 命名空间),我们将不得不在这里维护需求.

as per above title, I was trying to handle POST request from XML HTTP in WCF. 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.

这是前端的代码(将发送请求的旧库)

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;

虽然这里是 WCF 的代码,但我没有处理它:

While here are the codes for the WCF, which I failed to handle it:

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;
        }

    }

在 web.config 中,我已经将绑定更改为customBinding"而不是webHttpBinding",因为我在请求的 RAW 格式而不是 JSON 格式上遇到错误.

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,但得到 xmlhttp status 400(错误请求).

After searching through the forum and other sites, I tried on changing the web.config accordingly but got xmlhttp status 400 (Bad Request) instead.

预先感谢您的建议.

推荐答案

尝试使用 Fiddler 监控您的请求并查看 RAW 请求的外观.它应该如下所示:

Try to monitor your request using Fiddler and see how the RAW request looks like. 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>

这篇关于在 WCF 中处理来自 XML HTTP 的 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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