当前位置: 首页 > 编程语言 > C#

C#LearningTutorial-PostingDataSharingviahttpWebRequest

时间:2023-04-10 19:15:28 C#

PostingDataviahttpWebRequestIneedto"post"somedatatoanexternalwebsiteusingtheHttpWebRequestobjectfrommyapplication(desktop)andreturntheresponsetomywebsiteviatheHttpWebRequestobjectapplication.However,thewebpagewherethedataispostedhastextboxeswithdynamicnames.HowtogetthenamesofthesetextboxesandpostdatainHttpWebRequest?例如,当我加载页面的文本框的名字就是这样U2FsdGVkX183MTQyNzE0MrhLOmUpqd3eL60xF19RmCwLlSiG5nC1H6wvtBDhjI3uM1krX_B8Fwc但是当我刷新页面名称更改这个U2FsdGVkX182MjMwNjIzMPAtotst_q9PP9TETomXB453Mq3M3ZY5HQt70ZeyxbRb118Y8GQbgP8。Thankyouforanysuggestions.varrequest=WebRequest.Create("http://foo");request.Method="POST";request.ContentType="application/x-www-form-urlencoded";使用(varwriter=newStreamWriter(request.GetRequestStream())){writer.Write("field=value");您可以通过XPath识别这些名称,例如用户是这样的:byte[]data=newASCIIEncoding().GetBytes("textBoxName1=blabla");HttpWebRequesthttpWebRequest=(HttpWebRequest)WebRequest.Create("http://localhost/myservlet");httpWebRequest.Method="POST";httpWebRequest.ContentType="application/x-www-form-urlencoded";httpWebRequest.ContentLength=data.Length;流myStream=httpWebRequest.GetRequestStream();myStream.Write(data,0,data.Length);myStream.Close();看起来你必须使用HttpWebRequest获取页面并解析相应的HttpWebResponse的内容以找出文本框的名称。然后使用另一个HttpWebRequest将值提交到页面。基本上您需要做的是:使用GET方法向带有文本框的页面所在的URL发出HttpWebRequest获取HttpWebResponse的响应流解析响应流中包含的页面并获取文本框的名称.您可以使用HTMLAgilityPack来实现这一点。使用POST方法发出HttpWebRequest,内容类型设置为“application/x-www-form-urlencoded”,键值对作为内容。问题的第一部分:也许HTML树是稳定的。然后你可以通过XPath找到你感兴趣的文本框。使用XmlReader、XDocument和Linq来完成它。我使用此功能发布数据。但是你传递的url必须格式为http://example.com/login.php?userid=myid&password=somepassword以上是C#学习教程:通过httpWebRequest发布数据分享的所有内容,如果对大家有用并且需要了解更多C#学习教程,希望大家多多关注—PrivateFunctionGetHtmlFromUrl(ByValurlAsString)AsStringIfurl.ToString()=vbNullStringThenThrowNewArgumentNullException("url","Parameterisnullorempty")EndIfDimhtmlAsString=vbNullStringDimrequestAsHttpWebRequest=WebRequest.Create(url)request.ContentType="Content-Type:application/x-www-form-urlencoded"request.Method="POST"尝试昏暗responseAsHttpWebResponse=request.GetResponse()DimreaderAsStreamReader=NewStreamReader(response.GetResponseStream())html=Trim$(reader.ReadToEnd)GetHtmlFromUrl=htmlCatchexAsWebExceptionGetHtmlFromUrl=ex.MessageEndTryEndFunction代表位置,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: