J'essaie de comprendre ce que je fais mal ici. Nous utilisons Service Now et j'essaie d'automatiser la soumission d'un des formulaires sur le site qui ne dispose pas d'une API que je puisse appeler. Le code source de forme ressemble:Soumettre un formulaire Web sur Service Now via .Net
<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>
On dirait un assez simple forme, donc je construit le code suivant pour gérer:
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();
}
Et j'utilise la classe aide de MimePart de http://aspnetupload.com qui est juste un simple assistant:
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; }
}
Je ne reçois aucun message d'erreur, je ne reçois rien du tout et rien ne se passe. Le WebResponse a une teneur longueur de -1, et en-têtes:
+ 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
Je ne sais pas quel chemin à parcourir d'ici, des suggestions?
Oui, mais le webservice ne fait pas la même chose que l'importation fait. L'importation tire dans le sys_id afin que je puisse persévérer les relations entre les éléments. – VydorScope