如何使用带有查询字符串的WebRequest发布数据 [英] How to Post Data Using WebRequest with Query String
本文介绍了如何使用带有查询字符串的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参数实用工具 [
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("http://www.xyz.com","postvar=1&postvar=2")
这篇关于如何使用带有查询字符串的WebRequest发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文