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

编程入门 行业动态 更新时间:2024-10-26 15:16:30
本文介绍了在 WCF 中处理来自 XML HTTP 的 POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

大家好,

根据上面的标题,我试图在 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/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/mPlayer"xmlns:i="http://www.w3/2001/XMLSchema-instance"><details>Ashu|29|7 Years|.NET</details></RequestData>

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

更多推荐

[db:关键词]

本文发布于:2023-04-26 19:44:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:XML   WCF   POST   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!