c#用javascript字段发送帖子数据

时间:2012-09-19 20:07:01

标签: c# javascript http post httpwebresponse

我的按钮上有这样的代码

private String readHtmlPage2(string url)
        {

            //setup some variables

            String country = "6";
            String city = "95";
            String lang = "17";
            String wtf = "ctl00$tresc$upPlacowka|ctl00$tresc$cbListaPlacowek";

            //setup some variables end

            String result = "";
            String strPost = "ctl00$tresc$cbListaKrajow=" + country + "&ctl00$tresc$cbListaPlacowek=" + city + "&ctl00$ddlWersjeJezykowe=" + lang + "&ctl00$tresc$ScriptManager1" + wtf;
            StreamWriter myWriter = null;

            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.Method = "POST";
            objRequest.ContentLength = strPost.Length;
            objRequest.ContentType = "application/x-www-form-urlencoded";

            try
            {
                myWriter = new StreamWriter(objRequest.GetRequestStream());
                myWriter.Write(strPost);
            }
            catch (Exception e)
            {
                return e.Message;
            }
            finally
            {
                myWriter.Close();
            }

            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
            using (StreamReader sr =
               new StreamReader(objResponse.GetResponseStream()))
            {
                result = sr.ReadToEnd();

                // Close and clean up the StreamReader
                sr.Close();
            }
            return result;
        }

        public static string PostData(string file, string data)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(file);
            request.Method = "POST";
            request.AllowAutoRedirect = true;
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] EncodedPostParams = Encoding.UTF8.GetBytes(data);
            request.ContentLength = EncodedPostParams.Length;
            request.GetRequestStream().Write(EncodedPostParams,
                                             0,
                                             EncodedPostParams.Length);
            request.GetRequestStream().Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string html = new StreamReader(response.GetResponseStream(),
                                           Encoding.UTF8).ReadToEnd();
            return html;
        }

我需要在我们的页面上发送帖子数据,但主要的麻烦是表单是动态的,某些字段看起来像这样:

<select name="ctl00$tresc$cbListaKrajow" onchange="javascript:setTimeout('__doPostBack(\'ctl00$tresc$cbListaKrajow\',\'\')', 0)" id="ctl00_tresc_cbListaKrajow" style="width:400px;">
    <option value="-1"></option>
    <option selected="selected" value="6">Białoruś</option>

</select>

我需要告诉我的代码必须选择此字段,它会提供所有值...所有表单,您可以在我们的页面上看到:https://by.e-konsulat.gov.pl/default.aspx

我发送了我的数据,但没有任何反应......如何提交此表单? (然后我将被重定向)

0 个答案:

没有答案