如何使用带有查询字符串的WebRequest发布数据

编程入门 行业动态 更新时间:2024-10-27 04:30:53
本文介绍了如何使用带有查询字符串的WebRequest发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨 我想使用Querystring形式使用System.Net.Webrequest发布数据 可能吗 ?如果是,那么任何人都可以给我该

Hi I want to post data using System.Net.Webrequest in the form of Querystring Is it Possible ? If yes then can anybody give me the code for that

推荐答案

的代码当然可以.从客户端的角度来看,没有查询字符串之类的东西.这只是URL的一部分. 这项工作看起来很有趣,可以很好地处理查询参数: WebRequest参数实用工具 [ ^ ].
—SA
Of course it is possible. From the client stand point, there is no such thing as query string; this is just part of the URL. This work looks like an interesting supplement to handle query arguments nicely: WebRequest Parameter Utility[^].
—SA

在这里,我们去.. Here we go.. using System.Net; using System.IO; public string Post(string url, string data) { string vystup = null; try { //Our postvars byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data); //Initialisation, we use localhost, change if appliable HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url); //Our method is post, otherwise the buffer (postvars) would be useless WebReq.Method = "POST"; //We use form contentType, for the postvars. WebReq.ContentType = "application/x-www-form-urlencoded"; //The length of the buffer (postvars) is used as contentlength. WebReq.ContentLength = buffer.Length; //We open a stream for writing the postvars Stream PostData = WebReq.GetRequestStream(); //Now we write, and afterwards, we close. Closing is always important! PostData.Write(buffer, 0, buffer.Length); PostData.Close(); //Get the response handle, we have no true response yet! HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); //Now, we read the response (the string), and output it. Stream Answer = WebResp.GetResponseStream(); StreamReader _Answer = new StreamReader(Answer); vystup = _Answer.ReadToEnd(); //Congratulations, you just requested your first POST page, you //can now start logging into most login forms, with your application //Or other examples. } catch (Exception ex) { } return vystup.Trim() + "\n"; } Post("www.xyz","postvar=1&postvar=2")

更多推荐

如何使用带有查询字符串的WebRequest发布数据

本文发布于:2023-10-11 13:52:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何使用   数据   WebRequest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!