2011-09-14 2 views
0

여기에서 내가 뭘 잘못하고 있는지 알아 내려고 노력 중입니다. 우리는 Service Now를 사용하며, 내가 부를 수있는 API가없는 사이트에서 양식 중 하나를 제출하는 것을 자동화하려고합니다. 지금 서비스 .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> 

간단한 충분한 양식을 것 같다, 그래서 그것을 처리하기 위해 다음 코드를 내장 : 같은 형태의 소스 코드가 보이는

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; } 
    } 

아무런 오류 메시지가 나타나지 않고 아무 것도 일어나지 않습니다. 내가 여기에서 이동하는 방법을 모르는

+  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 

, 어떤 제안 : WebResponse를이 콘텐츠 -1의 아이폰에, 그리고 헤더를 가지고?

답변