在C#中构造POST [英] Constructing a POST in C#

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

问题描述

我使用在stackoverflow上找到的PostData类编写了以下代码,以构建POST.

I've written the following code in order to construct a POST, using the PostData class I found on stackoverflow.

        PostData pd = new PostData();
        pd.Params.Add(new PostDataParam("sessionId", "0", PostDataParamType.Field));
        pd.Params.Add(new PostDataParam("guestId", "1", PostDataParamType.Field));

        HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(new Uri("http://oe1235/test/uploadTest.php").AbsoluteUri);

        webrequest.ContentType = "multipart/form-data; boundary=" + pd.Boundary;
        webrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        webrequest.Headers.Add("Accept-Language: en-gb,en;q=0.5");
        webrequest.Headers.Add("Accept-Encoding: gzip,deflate");
        webrequest.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        webrequest.Headers.Add("Keep-Alive: 115");
        webrequest.Referer = "http://localhost/test/test.php";
        webrequest.Headers.Add("Cache-Control: max-age=0");
        webrequest.Method = "POST";


        byte[] content = Encoding.ASCII.GetBytes(pd.GetPostData());
        webrequest.ContentLength = content.Length;
        Stream request = webrequest.GetRequestStream();
        request.Write(content, 0, content.Length);
        try {
            Console.Write(webrequest.GetResponse());
        } catch (Exception e) {
            Console.Write("Error: " + e.ToString());
        }
        Console.ReadLine();

我正在监视Charles的请求/响应,并且得到以下信息:

I'm monitoring the request / response with Charles, and I get the following:

POST /test/uploadTest.php HTTP/1.1
Content-Type: multipart/form-data; boundary=----------8cd4899a18b409a
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Referer: http://localhost/test/test.php
Cache-Control: max-age=0
Host: oe1235
Content-Length: 275
Expect: 100-continue

----------8cd4899a18b409a
Content-Disposition: form-data; name="email"

MyEmail
----------8cd4899a18b409a
Content-Disposition: form-data; name="sessionId"

0
----------8cd4899a18b409a
Content-Disposition: form-data; name="guestId"

1
----------8cd4899a18b409a--

我已经尝试了所有可以想到的方法,但是它似乎仍然无效(我的POST接收器没有响应,Charles报告无法解码Multipart主体",所以我做错了什么)我的边界看起来不错,我在所有正确的位置都有换行符,并将其与Firefox构建的POST进行了比较,并且它们看起来(除了一个或两个标题之外,看起来非常相同).

I've tried everything I can think of, but it still doesn't appear to be valid (no response from my POST-receiver, and Charles reports "Failed to decode Multipart body", so I've done something wrong. My boundary looks ok, I've got line breaks in all the correct places, I've compared it to a POST that Firefox constructs and they look (pretty much - apart from one or two headers) identical.

推荐答案

解决了这个问题-我假设指定边界时,它应该包括两个前导字符-而不是.因此,在内容标头上指定的边界应为:

Worked it out - I assumed that when the boundary was specified, it should include the two leading -- characters, when it shouldn't. So my boundary specified on the content header should be:

----------8cd4899a18b409a

,然后在每个项目之间:

and then between each item:

------------8cd4899a18b409a 

更简单地说,如果您的边界是:

More simply, if your boundary was:

Content-Type: multipart/form-data; boundary=myboundary

然后在每个项目之间应该是:

Then between each item it should be:

--myboundary

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

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