错误500:路径中的非法字符

时间:2016-03-06 12:13:41

标签: c# xml

我正在尝试将一些数据发布到EndPoint,从而产生响应 - illegal characters in path

RootDTO = DTO结构,我将示例XML粘贴到this site 所以我认为这不是问题所在。

我做错了吗?

private Boolean ApiRequest(string order, string path)
{
    var t = string.Empty;
    var Obj_response = new RootDTO();

    byte[] requestData = Encoding.UTF8.GetBytes(order);

    HttpWebRequest req = WebRequest.Create(new Uri(path)) as HttpWebRequest;
    req.Method = "POST";
    req.Accept = "application/xml";
    req.ContentType = "application/xml; charset=utf-8";
    req.ContentLength = requestData.Length;

    using (Stream post = req.GetRequestStream())
    {
        post.Write(requestData, 0, requestData.Length);
    }

    try
    {
        //XmlSerializer serializer = new XmlSerializer(typeof(RootDTO));

        using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
        {
            string resJson = string.Empty;

            if (resp.StatusCode == HttpStatusCode.OK)
            {
                var ms = new StreamReader(resp.GetResponseStream(), UTF8Encoding.UTF8);
                t = ms.ReadToEnd(); // <-----t contains "illegal characters in path"

                return true;
            }
            else
            { // error occured?
                return false;
            }
        }
    }
    catch (WebException ex)
    {
        // error during APIrequest
        var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
    }

    return false;
}

1 个答案:

答案 0 :(得分:0)

答案很容易解决 - XML中存在问题

导致问题的原因是:

<url>
http://www.domain.com
</url>

我把它改为:

 <url>http://www.domain.com</url>

修复它。