我有一個C#Windows窗體應用程序,我正在使用它來連接到一個用於製作Web請求的服務器。我需要做的是讓用戶通過首選項設置某些屬性,並將這些屬性動態添加到WebRequest。向HttpWebRequest動態添加屬性
一樣,如果我有一個配置文件與條目 - >
<Properties>
<Property name="User-Agent" value="Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" />
<Property name="KeepAlive" value="true" />
</Properties>
現在我想將值綁定到WebRequest的屬性。
Uri serverURL = new Uri("http://MyServer:8080/MyPage.jsp");
HttpWebRequest wreq = WebRequest.Create(serverURL) as HttpWebRequest;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(<Path of Config>);
XDocument xDoc = XDocument.Parse(xmldoc.InnerXml);
Dictionary<string, string> propdict = new Dictionary<string, string>();
foreach (var section in xDoc.Root.Elements("Property"))
{
propdict.Add(section.Attribute("name").Value, section.Attribute("value").Value);
}
string key = string.Empty, value = string.Empty;
foreach (var item in propdict)
{
//... add the properties to wreq
}
有人可以讓我知道這可以實現嗎?
感謝
蘇尼爾Jambekar
'if(name ==「User-Agent」){request.UserAgent = value; }'? – dtb 2013-02-11 12:58:50