我想知道我在这里做错了什么。我们正在使用Service Now,我正在尝试自动提交网站上的一个表单,其中没有我可以调用的API。表单源代码如下所示:
<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>
看起来像一个简单的形式,所以我构建了以下代码来处理它:
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();
}
我正在使用http://aspnetupload.com中的mimepart助手类,这只是一个简单的助手:
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; }
}
我没有收到任何错误消息,我什么也得不回来,没有任何反应。 WebResponse的内容长度为-1,标题为:
+ 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
我不知道从这里走哪条路,有什么建议吗?
答案 0 :(得分:0)
您是否考虑过使用Web服务来实现此目标?
http://wiki.service-now.com/index.php?title=Direct_Web_Services