从字符串 (xml) 中删除转义字符并将其发送到 WCF 服务
我正在尝试向我的 WCF 服务发出 POST http 请求并以 XML 格式发送一些数据。我尝试使用 XmlWriter 和字符串连接来构建 XML 字符串,但我的字符串中总是有转义字符(\" 和其他字符),因此我的 POST 失败。
这就是我构建 XML 字符串的方式:
var data = string.Empty;
data += (@"<Root xmlns=""http://schemas.datacontract.org/2004/07/..."" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">");
data += (String.Format("<Element1>{0}</Element1>", element1));
data += (String.Format("<Element2>{0}</Element2>", element1));
data += (String.Format("<Element3>{0}</Element3>", element1));
data += (String.Format("<Element4>{0}</Element4>", element4));
data += (String.Format("</Root>"));
然后我得到 \"由于 xmlns 属性及其值,根元素中存在字符。这就是为什么我的 POST 总是返回 400 Bad Request。这就是我发布帖子的方式:
var req = WebRequest.Create(uri);
req.ContentType = "application/xml; charset=utf-8";
req.Method = "POST";
var bytes = Encoding.UTF8.GetBytes(data);
req.ContentLength = bytes.Length;
var os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Flush();
os.Close();
var response = _GetResponseString(req.GetResponse());
return response;
如何删除不需要的字符并使此帖子正常工作?
I'm trying to make a POST http request to my WCF Service and send ti some data in XML. I tried building an XML string both using XmlWriter and just string concatenation, but I always have escaping characters (\" and other) in my string, and so my POST fails.
This is how I build an XML String:
var data = string.Empty;
data += (@"<Root xmlns=""http://schemas.datacontract.org/2004/07/..."" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">");
data += (String.Format("<Element1>{0}</Element1>", element1));
data += (String.Format("<Element2>{0}</Element2>", element1));
data += (String.Format("<Element3>{0}</Element3>", element1));
data += (String.Format("<Element4>{0}</Element4>", element4));
data += (String.Format("</Root>"));
and I get \" characters in Root element because of xmlns attribute and its value. That's why my POST alaways returns 400 Bad Request. This is how I make a POST:
var req = WebRequest.Create(uri);
req.ContentType = "application/xml; charset=utf-8";
req.Method = "POST";
var bytes = Encoding.UTF8.GetBytes(data);
req.ContentLength = bytes.Length;
var os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Flush();
os.Close();
var response = _GetResponseString(req.GetResponse());
return response;
How to get rid of unwanted characters and make this POST working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您在评论中确认的那样,问题不在于转义字符(VS 中的监视窗口将它们显示为好像它们被转义一样,这通常是一个转移注意力的问题),而是其他问题。
几乎每次您都会遇到 WCF 服务向客户端返回 400 / 500 错误的问题,启用跟踪应该会为您指明解决问题的正确方向。
As you confirmed in the comments, the problem wasn't the escaped characters (it's usually a red herring that the watch window in VS shows them as if they were escaped), but something else.
Almost all the time where you have a problem where a WCF service is returning a 400 / 500 error to the client, enabling tracing should point you to the right direction in fixing the issue.