开发者

Submitting a web form on Service Now via .Net

I am trying to figure out what I am doing wrong here. We use Service Now and I am trying to automate the submission of one of the forms on the site that does not have an API behind it that I can call. The form source code looks like :

<FORM action="sys_upload.do" method="post" enctype="multipart/form-data">
<input value="u_authoritative_source_list.do" type="hidden" name="sysparm_referring_url"></input>
<input value="u_authoritative_source" type="hidden" name="sysparm_target"></input>
<input id="attachFile" type="file" size="41" name="attachFile"></input>
<DIV class="caption">(2) Upload the file</DIV>
<input value="Upload" style="width: 85px;" type="submit" width="85"></input>
</FORM>

Seems like a simple enough form, so I built the following code to handle it:

public static void importXML(string fileName)
    {
        List<MimePart> mimeParts = new List<MimePart>();

        NameValueCollection form = new NameValueCollection();


        //build the form values
        form["sysparm_referring_url"] = "u_authoritative_source_list.do";
        form["sysparm_target"] = "u_authoritative_source";
        form["submit"] = "Upload";
        form["Upload"] = "";

        foreach (string key in form.AllKeys)
        {
            StringMimePart part = new StringMimePart();

            part.Headers["Content-Disposition"] = "form-data; name=\"" + key + "\"";
            part.StringData = form[key];

            mimeParts.Add(part);
        }


        //add the file
        StreamMimePart part1 = new StreamMimePart();
        part1.Headers["Content-Disposition"] = "form-data; name=\"" + "attachFile" + "\"; filename=\"" + "attachFile" + "\"";
        part1.Headers["Content-Type"] = "application/octet-stream";
        FileStream theFile = new FileStream(fileName, FileMode.Open);
        part1.SetStream(theFile);
        mimeParts.Add(part1);

        //uild sending package
        string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
        System.Net.ICredentials cred = new System.Net.NetworkCredential(Properties.Settings.Default.UserName, Properties.Settings.Default.Password);
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urlStr);
        webRequest.Method = "POST";
        long contentLength = 0;
        webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
        byte[] _footer = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");

        foreach (MimePart part in mimeParts)
        {
            contentLength += part.GenerateHeaderFooterData(boundary);
        }
        webRequest.ContentLength = contentLength + _footer.Length;
        webRequest.AllowWriteStreamBuffering = true;
        webRequest.Credentials = cred;

        byte[] buffer = new byte[8192];
        byte[] afterFile = Encoding.UTF8.GetBytes("\r\n");
        int read;

        using (Stream s = webRequest.GetRequestStream())
        {
            foreach (MimePart part in mimeParts)
            {
                s.Write(part.Header, 0, part.Header.Length);

                while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
                    s.Write(buffer, 0, read);

                part.Data.Dispose();

                s.Write(afterFile, 0, afterFile.Length);
            }

            s.Write(_footer, 0, _footer.Length);
        }
        foreach (MimePart part in mimeParts)
            if (part.Data != null)
                part.Data.Dispose();
        WebResponse response = webRequest.GetResponse();
        string res = response.ToString();
    }

And I am using the mimepart helper 开发者_StackOverflow中文版class from http://aspnetupload.com which is just a simple helper:

public abstract class MimePart
    {
        NameValueCollection _headers = new NameValueCollection();
        byte[] _header;

        public NameValueCollection Headers
        {
            get { return _headers; }
        }

        public byte[] Header
        {
            get { return _header; }
        }

        public long GenerateHeaderFooterData(string boundary)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("--");
            sb.Append(boundary);
            sb.AppendLine();
            foreach (string key in _headers.AllKeys)
            {
                sb.Append(key);
                sb.Append(": ");
                sb.AppendLine(_headers[key]);
            }
            sb.AppendLine();

            _header = Encoding.UTF8.GetBytes(sb.ToString());

            return _header.Length + Data.Length + 2;
        }

        public abstract Stream Data { get; }
    }

I get no error message, I get nothing back at all and nothing happens. The WebResponse has a content lenght of -1, and headers :

+       Headers {Pragma: no-store,no-cache
Cache-Control: no-cache,no-store,must-revalidate,max-age=-1
Content-Type: text/html; charset=UTF-8
Date: Wed, 14 Sep 2011 12:49:26 GMT
Expires: 0
Set-Cookie: JSESSIONID=8C9E1391E8F71AE2AA18AD5BB065683A; Path=/,glide_user_route=glide.fd4bdfa50a0a3c69006e8f94f6467f1a; Expires=Mon, 02-Oct-2079 16:03:34 GMT; Path=/
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked

}   System.Net.WebHeaderCollection

I do not know which way to go from here, any suggestions?


Have you considered using Web Services to accomplish this?

http://wiki.service-now.com/index.php?title=Direct_Web_Services

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜