2011-01-27 37 views
0

我有上傳文件到java servlet的任務。我安裝了Fiddler以查看Web請求發送的位置以及發送的數據。在使用HttpWebRequest GET方法登錄到java servlet後,我通過Cookie SessionId接收。所以我在頭文件中使用這個SessionId來創建POST請求到servlet所在的web服務器。但作爲迴應,我收到「會話超時,嘗試重新登錄」的消息。但是,如果我通過用戶界面使用應用程序,那麼對於每個請求在標頭中發送的所有應用程序,我都有一個SessionId。 這個應用程序運行在銀行,所以我想如果他們有一定的安全性,以防刮傷。 我正在以正確的方式思考?任何幫助將不勝感激。 謝謝,埃琳娜使用C#發送郵件到java servlet HttpWebRequest

這裏是我的代碼

CookieContainer cookieContainer = new CookieContainer(); 

     HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://go.tkygw.pcnet.smbc.local/MgbTokyoGateway/Mgblinkmenu.aspx"); 
     req.Credentials = new NetworkCredential("GB54326", "elena83", "TKYGW"); 
     req.CookieContainer = cookieContainer; 
     req.Headers.Add("Pragma", "no-cache"); 
     req.Headers.Add("Accept-Language", "en-gb"); 
     req.ProtocolVersion = HttpVersion.Version10; 
     req.AllowAutoRedirect = true; 
     WebResponse resp = req.GetResponse(); 
     //here in cookies I receive ASP.NET_session_Id and tkygw_intra 
     HttpWebResponse webr = (HttpWebResponse)resp; 
     StreamReader r = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8); 
     string res = r.ReadToEnd(); 
     resp.Close(); 

     NameValueCollection nvc = new NameValueCollection(); 
     nvc.Add("_PAGEID", "MWMAL1000P00"); 
     nvc.Add("_SENDTS", "1296208904759"); 
     nvc.Add("_TRANID", "AL1000T00P01"); 
     nvc.Add("_SUBINDEX", "-1"); 
     nvc.Add("_TARGET", ""); 
     nvc.Add("_FRAMID", ""); 
     nvc.Add("_LUID", "1296208904720"); 
     nvc.Add("_WINID", "root"); 
     nvc.Add("_TARGETWINID", "TIMEOUTW_300000_13"); 
     nvc.Add("CHK_FLG", "0"); 
     nvc.Add("BUTTON_NAME", "Corporate Card"); 
     nvc.Add("TITLE_NAME", "[AL1000]Main Menu"); 
     nvc.Add("DateFormat", "1"); 
     nvc.Add("BIZKEY", "AC"); 
     nvc.Add("H_REG_NUM", ""); 
     nvc.Add("H_TODO_DISP_MODE", ""); 
     nvc.Add("H_VIEW_CHANGE_FLG", "1"); 
     nvc.Add("H_SMVA_FLG", "0"); 
     nvc.Add("H_SWITCH_ID", "8837"); 
     nvc.Add("T_BOOKING", "8802"); 
     nvc.Add("T_CUSTOMER_ID", "109732"); 
     nvc.Add("P_DATE_FM", "1"); 
     nvc.Add("D_BA_CREDIT_MONITORING_DISABLED", ""); 
     nvc.Add("D_BA_CREDIT_APPLICATION_DISABLED", ""); 
     nvc.Add("D_BA_CREDIT_APPLICATION_DISABLED", ""); 
     nvc.Add("P_BLANKET_APPLI", ""); 

     HttpWebRequest req3 = (HttpWebRequest)WebRequest.Create("http://gcm.tkygw.pcnet.smbc.local/gcmv0/WACSServlet"); 
     //here in cookiesContainer are 4 values: ASP.NET_session_Id , tkygw_intra 
     req3.CookieContainer = cookieContainer; 
     req3.Method = "POST"; 
     req3.Accept = "*/*"; 
     // req3.Headers.Add("Pragma", "no-cache"); 
     // req3.Headers.Add("Accept-Language", "en-gb"); 
     req3.AllowAutoRedirect = true; 
     req3.KeepAlive = true; 
     req3.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"; 
     req3.ContentType = "application/x-www-form-urlencoded"; 
     req3.ProtocolVersion = HttpVersion.Version10; 
     var sbPostData = new StringBuilder(); 

     if (nvc != null) 
     { 
      foreach (string key in nvc.AllKeys) 
      { 
       string[] values = nvc.GetValues(key); 
       if (values != null) 
       { 
        foreach (string value in values) 
        { 
         if (!string.IsNullOrEmpty(value)) 
          sbPostData.Append(string.Format("{0}={1}&", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value))); 
        } 
       } 
      } 
     } 
     var parameterString = Encoding.UTF8.GetBytes(sbPostData.ToString()); 
     req3.Referer = "http://gcm.tkygw.pcnet.smbc.local/gcmv0/WACSServlet?_TRANID=AL0010P01C01"; 
     req3.ContentLength = sbPostData.ToString().Length; 

     using (Stream requestStream = req3.GetRequestStream()) 
     { 
      requestStream.Write(parameterString, 0, parameterString.Length); 
      requestStream.Close(); 

      //nothig is received in cookies. Status of response 200 (OK), but on the web page is error that Session is Time OUT. Please Login again 
      using (var response = req3.GetResponse() as HttpWebResponse) 
      { 
       using (var stIn = new System.IO.StreamReader(response.GetResponseStream())) 
       { 
        //here I receive session Time Out. Please login again 
        string s = stIn.ReadToEnd(); 
       } 
      } 
     } 
+0

您具體是如何在請求頭中設置會話ID?我可以假設它看起來像這個`Cookie:JSESSIONID = somehexvalueherehere`嗎?你有控制servlet的源代碼嗎? – BalusC 2011-01-27 21:05:28

+0

我正在使用CookieContainer。我在我的問題中添加了代碼。我沒有控制servlet代碼 – Elena 2011-01-28 12:12:39

回答

0

如果您正在使用HttpWebRequest的上傳文件時,必須指定內容頭部是正確的。

內容必須被指定爲內容類型:多重/ form-data的

示例代碼段

string DataBoundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x"); 
    string contentType = "multipart/form-data; boundary=" + DataBoundary ; 
    req.Method = "POST"; 
    req.ContentType = contentType ; 
    req.UserAgent = userAgent; 
    req.CookieContainer = new CookieContainer(); 
    req.ContentLength = formData.Length; 

退房thisthis職位更詳細的解釋