使用C#HttpWebRequest将帖子发送到java servlet

时间:2011-01-27 20:28:43

标签: c# .net servlets httpwebrequest httpwebresponse

我有将文件上传到java servlet的任务。我安装了Fiddler以查看Web请求的发送位置以及发送的帖子数据。使用HttpWebRequest GET方法登录java servlet后,我在cookies SessionId中收到。所以我在头文件中使用这个SessionId来创建对servlet所在的Web服务器的POST请求。但作为回应,我收到消息“会话超时。尝试再次登录。”但是,如果我通过用户界面使用应用程序,我将为所有应用程序提供一个SessionId,并在每个请求的头文件中发送。 这个应用程序在银行运行,所以我在想他们是否有一些防止抓取的安全性。   我是否以正确的方式思考?任何帮助将不胜感激。 谢谢,艾琳娜

这是我的代码

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

1 个答案:

答案 0 :(得分:0)

如果使用HttpWebRequest上传文件,则必须指定内容标题正确。

内容必须指定为内容类型:multipart / 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帖子以获取更多详细说明