Handle POST request from XML HTTP in WCF

ぐ巨炮叔叔 提交于 2020-02-23 06:51:17

问题


Hi and good day everyone,

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;

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

    }

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.

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

Thanks in advance for the advices.


回答1:


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>



回答2:


thanks for the reply. I changed Content-Type header:

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

It works! I now can change the xml input to the WCF and get the status 200.

However, for the monitoring using Fiddler, I did open up Fiddler, but I am not sure if I got this right:

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?

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:

[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. Would it be possible to remove namespace in first place?

Thanks

Edited: I change DataContract to:

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

and managed to get HTTP status to 200! Now I am trying to change the request xml to the one that our existing application use.



来源:https://stackoverflow.com/questions/9887002/handle-post-request-from-xml-http-in-wcf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!