帮助我在C#中正确创建POST查询 [英] Help me to create POST query correctly in C#

查看:64
本文介绍了帮助我在C#中正确创建POST查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我想用C#编写一个程序,该程序将自动创建电子邮件帐户,为此,我编写了一个验证码阅读器类.现在,我想创建一个POST命令,该命令会将有关用户的数据发送到服务器.如何正确确定命令格式?
这是我写的:

Hello everybody! I want to write a program in C# that will automatically create email accounts and for this purpose I wrote a captcha reader class. Now I want to create a POST command that will send to the server the data about user. How can I correctly determine the format of the command?
HEre is what i wrote:

//-------------------------------------------------------------------------------
//Create HttpWebRequest instance
            HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(url);
//specify the method
            postRequest.Method = "Post";
            postRequest.ContentType = "application/x-www-form-urlencoded";
//create namevalue collection for sending to the server name value pairs (is it //correctly?)
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("_token", sessionID);
            nvc.Add("_uname", "lalala123");
            .........
//I dont know how should I exactly create the message
            byte[] btPostData = Encoding.ASCII.GetBytes("_token" + nvc.Get("_token"));

            postRequest.ContentLength = btPostData.Length;
            Stream postData = postRequest.GetRequestStream();
            postData.Write(btPostData, 0, btPostData.Length);
            HttpWebResponse response2 = (HttpWebResponse)postRequest.GetResponse();
            Stream responseStream2 = response.GetResponseStream();
            StreamReader reader2 = new StreamReader(responseStream2);
            String response22 = reader2.ReadToEnd();
            ;
//-------------------------------------------------------------------------------


提前谢谢!

[edit]已添加代码块-OriginalGriff [/edit]


Thanks in advance!

[edit]Code block added - OriginalGriff[/edit]

推荐答案



我不知道您想要(需要)发送什么参数,但是一般而言,如何创建post params可以找到 ^ ].网上还有许多其他示例.

通常,帖子数据的格式与其他任何查询字符串一样(Key1 = Value1& Key2 = Value2等等...)
您的帖子数据将如下所示: _token = 100& _uname = lalala123
同样,在创建发布数据时,您需要使用
Hi,

I don''t know what parameters you want (need) to send but how create post params in general you can find here[^]. There are many other examples on net.

In general post data is formatted like any other query string (Key1=Value1&Key2=Value2 and so on...)
Yours post data will look like this: _token=100&_uname=lalala123
Also when you creating post data you need to encode values using
System.Web.HttpUtility.UrlEncode()

方法对值进行编码.
这几乎是您成功完成发帖请求所需要的全部...

method.
And that''s pretty much all you need to make successful post request...


这篇关于帮助我在C#中正确创建POST查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆