简体   繁体   English

如何通过QTP Web服务提交无效的XML

[英]How to submit invalid XML through QTP Web Services

I am trying to submit an XML using the web services tool in QTP. 我正在尝试使用QTP中的Web服务工具提交XML。 I can submit valid XML's with no problem but when I try to submit an invalid XML for negative testing (such as setting an elements value to 'XXX' when it should be a valid date). 我可以毫无问题地提交有效的XML,但是当我尝试提交无效的XML进行负面测试时(例如,当元素值应为有效日期时将其设置为“ XXX”)。 I keep getting an error on the line 我一直在线出错

Set submitXMLRequest = WebServices(webservicename).submitRequest(subReq) 设置SubmitXMLRequest = WebServices(webservicename).submitRequest(subReq)

The error says "There is an error in XML document(92,8) Exception from: mscorlib String was not recognized as a valid Date Time 该错误显示“ XML文档中存在错误(92,8),来自以下错误:mscorlib字符串未被识别为有效的日期时间

How do I prevent the XML's data from being validated before the request is submitted? 如何防止在提交请求之前验证XML的数据?

Have you tried hitting the web service through QTP without using the web services tool? 您是否尝试过通过QTP而不使用Web服务工具来访问Web服务? I run all of our web services through through QTP/UFT without that portion of the suite and I can submit whatever I want to our APIs. 我通过QTP / UFT运行我们所有的Web服务,而没有套件的那一部分,我可以将所需的任何内容提交给我们的API。

I've had to check the response after submitting malformed XML/JSON data to ensure that checks were being completed prior to it being processed by the main service and that sounds like what you are trying to do. 提交格式错误的XML / JSON数据后,我必须检查响应,以确保检查在主服务处理之前已经完成,这听起来像您正在尝试做的事情。

Not having actually used that tool, could it be validating your request prior to submitting it? 没有实际使用该工具,它可以在提交请求之前验证您的请求吗? If that's the case, it may not be possible to inject bad request data. 如果是这种情况,则可能无法注入错误的请求数据。

I believe you are using UFT API Module. 我相信您正在使用UFT API模块。 UFT parses the XML and hence you will not be able to enter XML special characters & and < in data portion. UFT解析XML,因此您将无法在数据部分输入XML特殊字符&和<。 You can use below function to do xml request post. 您可以使用以下功能来执行xml请求发布。 This is what I am using in my project now. 这就是我现在在项目中使用的。

public static string HttpRequest(string url, string xml)
    {
        string response = string.Empty;

        HttpWebRequest httpWebRequest = null;
        HttpWebResponse httpWebResponse = null;

        httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

        httpWebRequest.Timeout = 10000;

        try
        {
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(xml);

            httpWebRequest.Method = "POST";
            httpWebRequest.ContentLength = bytes.Length;
            httpWebRequest.ContentType = "text/xml; encoding='utf-8'";

            using (Stream requestStream = httpWebRequest.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
            }

            httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            if (httpWebResponse.StatusCode == HttpStatusCode.OK)
            {
                using (Stream responseStream = httpWebResponse.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                        response = reader.ReadToEnd();
                }
            }
            httpWebResponse.Close();
        }
            catch (Exception ex)
            {
                throw new Exception ("Error");
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                    httpWebResponse = null;
                }

                httpWebRequest = null;
            }
            return response;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM