大家好,祝大家好,在WCF處理來自XML HTTP的POST請求
根據上述標題,我試圖處理來自WCF中XML HTTP的POST請求。實際上,我已經可以使用HTTP Web Request處理請求,但由於舊庫以這種形式發送請求(如代碼中所示,我們使用的是MSXML2名稱空間),因此我們必須在此處維護此要求。
這裏是前端代碼(舊庫,將請求發送)
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,我沒能處理:
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格式的錯誤。
通過論壇和其他網站搜索後,我試着相應地改變web.config,但得到了xmlhttp status 400(Bad Request)。
在此先感謝您的建議。
您是否試圖使用Fiddler監視您的請求?此外,RAW消息是由於您的請求未設置Content-Type標題屬性。只需將其設置爲application/xml或application/json,即可得到解決 – Rajesh 2012-03-27 11:28:58