Skip to main content
 首页 » 编程设计

c#之如何从 HttpWebRequest 中的 DropDownList 中进行选择

2024年01月13日32lovecherry

1.HttpWebRequest+HttpWebResponse 登录然后转到“ex.asps”页面 2.发送 HttpWebRequest(从 ddl 中选择选项)+HttpWebRespons 以保存我需要的所有信息到 StreamReader。

1.我成功了,我看到所有信息都有效 2.我看到了dll(Id,所有选项,值,名称)但是我找不到选择正确选项的方法我只能看到并保存第一个,但我需要一个一个地选择所有ddl并保存数据。 例如:像 ddl 一样有 4 个用户,每个用户都有自己的数据,比如 (Age,ID...) 我只能保存 1 个用户,但我需要更改 ddl 以获取新数据

这是我的代码

if (IsLogin) 
                            { 
          HttpWebResponse RedirectResponse = RedirectToUrl("https://services.test.com/Pages/Trans.aspx"); 
 
Stream streamResponse = RedirectResponse.GetResponseStream(); 
StreamReader streamRead = new StreamReader(streamResponse); 
outString = streamRead.ReadToEnd(); 
 
System.Collections.ArrayList strAccountList = GetListByID("ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts"); 
for (int intAccountCount = 0; intAccountCount < strAccountList.Count; intAccountCount++) 
          { 
string[] strAcctList = strAccountList[intAccountCount].ToString().Split('\t'); 
string strAccountNumber = strAcctList[0]; 
 
PostString += "__EVENTVALIDATION=" + GetValueByID(LoginInfo, "__EVENTVALIDATION") + "&"; 
PostString += "ctl00$PlaceHolderMain$AccountsDDL$ddlAccounts=" + strAccountNumber + "&"; 
 
HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create("https://services.test.com/currentaccount/Pages/current.aspx"); 
 
    postRequest.CookieContainer = new CookieContainer(); 
    postRequest.CookieContainer = _cookies; 
    postRequest.Method = WebRequestMethods.Http.Post; 
    postRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; 
    postRequest.AllowWriteStreamBuffering = false; 
    postRequest.ProtocolVersion = HttpVersion.Version11; 
    postRequest.AllowAutoRedirect = false; 
    postRequest.ContentType = "application/x-www-form-urlencoded"; 
 
    byte[] byteArray = Encoding.ASCII.GetBytes(PostString); 
    postRequest.ContentLength = byteArray.Length; 
    Stream newStream = postRequest.GetRequestStream(); 
    newStream.Write(byteArray, 0, byteArray.Length); 
    newStream.Close(); 
 
    HttpWebResponse postResponse = (HttpWebResponse)postRequest.GetResponse(); 
    outString = ""; 
    Stream streamResponseLoginForm = postResponse.GetResponseStream(); 
    StreamReader streamReadLoginForm = new StreamReader(streamResponseLoginForm); 
    outString = streamReadLoginForm.ReadToEnd();//Here i can see that data is not changed 
 
 
                                    if (outString == null) 
                                    { 
                                        LogOut(); 
                                    } 
 
                                }  

请您参考如下方法:

$("#ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts")[0].selectedIndex = 0;//Here we select the index 
__doPostBack('ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts','') // here the postback