C#发布到其他服务器 [英] C# post to different server

查看:177
本文介绍了C#发布到其他服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#并尝试将数据发布到另一台服务器上的表单中,该表单包含图像附件以及一些参数,例如:

firstName =第一 lastName =姓氏 图片= ImageFile.JPG

寻找有关如何构造发送此消息的方法的示例,我一直在研究HttpWebRequest,但需要一个示例.

解决方案

我现在可以进行此工作-我的测试是在ASP.NET托管的网页上进行的...但是基本技术应该可以在任何地方使用.

手动发布到Web服务器可能会有些棘手.我总是从像HttpWatch这样的好工具开始-我在页面上发布了一个真实的帖子,然后使用该工具查看它发布的内容,查看发送的标题和发送的cookie.

在这种情况下,标头中的内容类型设置为multipart/form-data并定义了边界.然后查看流,您可以看到字节是如何发送到服务器的.

完成这些步骤后,您只需要弄清楚如何从c#发送相同的字节即可.

此示例代码仅显示了一个有效的示例.显然,生产代码将更进一步:

1)它会首先连接以获得新的__VIEWSTATE和__EVENTVALIDATION参数,而不是像我在这里那样硬接线.

2)它会查看表单参数(fname,lname)并确认其名称.

3)它可能会寻找新的参数.

4)它可能每次都会生成boundry.不确定这一点,但是浏览器是通过这种方式完成的.

5)文件名将不会硬连线.

6)可能存在一种更有效的方法来创建最终的字节数组.

代码如下:

    private void SimulatePost_Click(object sender, EventArgs e)
    {
        WebClient client = new WebClient();

        const string boundry = "---------------------------7dc2a722a12c8";

        string contentType = string.Format("multipart/form-data; boundary={0}", boundry);

        string postData = string.Format(@"--{0}
Content-Disposition: form-data; name=""__VIEWSTATE""

/wEPDwUKLTk2MDkyMzQyMw9kFgJmD2QWAgIDDxYCHgdlbmN0eXBlBRNtdWx0aXBhcnQvZm9ybS1kYXRhZGQLrqV5FQTzi8K9ogSJlS44c0L0Ou3+MaYfFPwjKPwjZQ==
--{0}
Content-Disposition: form-data; name=""__EVENTVALIDATION""

/wEWBAKXx6zsBALa1ffGCwK80PHQDQLurOv8AU7Jo8sYj9+E/zw7RsmFraAotTazyvQc7T2VseLqSwGO
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$fname""

jim
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$lname""

bob
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$picUpload""; filename=""C:\temp\small.JPG""
Content-Type: image/jpeg

", boundry);

        string endData = string.Format(@"
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$post""


--{0}--
", boundry);


        List<byte> postByteArray = Encoding.UTF8.GetBytes(postData).ToList();

        byte[] file = File.ReadAllBytes(@"C:\temp\small.JPG");

        postByteArray.AddRange(file);

        postByteArray.AddRange(Encoding.UTF8.GetBytes(endData));

        client.Headers.Add("Content-Type", contentType);

        client.UploadData("http://localhost:63247/Default.aspx", "POST", postByteArray.ToArray());
    }   

I am using c# and trying to post data to a form on another server, the form includes an image attachment along with some parameters ex:

firstName = First lastName = Last Image = ImageFile.JPG

Looking for an example of how to construct a method to send this I've been looking into the HttpWebRequest but need an example.

解决方案

I have this working now - my test is with an ASP.NET hosted web page... but the basic technique should work anywhere.

Posting to web servers manually can get a bit tricky. I always start with a nice tool like HttpWatch - I make a real post to the page and then using the tool look at what it posted, and look at the headers sent, and look at the cookies sent.

In this case, the content type in the header is set to multipart/form-data and a boundary is defined. Then look at the stream and you can see how the bytes are being sent to the server.

Once those steps are done, you just have to figure out how to send the same bytes from c#.

This example code just shows one working example. Clearly, production code would take a few things a few steps farther:

1) It would connect first to obtain fresh __VIEWSTATE and __EVENTVALIDATION parameters as opposed to hardwiring them as I have here.

2) It would look at the form parameters (fname, lname) and confirm their names.

3) It might look for new for parameters.

4) It might generate boundry each time. Not sure on this one, but the browser does it this way.

5) The file name would not be hard wired.

6) There's probably a more efficient way to create the final byte array.

Here's the code:

    private void SimulatePost_Click(object sender, EventArgs e)
    {
        WebClient client = new WebClient();

        const string boundry = "---------------------------7dc2a722a12c8";

        string contentType = string.Format("multipart/form-data; boundary={0}", boundry);

        string postData = string.Format(@"--{0}
Content-Disposition: form-data; name=""__VIEWSTATE""

/wEPDwUKLTk2MDkyMzQyMw9kFgJmD2QWAgIDDxYCHgdlbmN0eXBlBRNtdWx0aXBhcnQvZm9ybS1kYXRhZGQLrqV5FQTzi8K9ogSJlS44c0L0Ou3+MaYfFPwjKPwjZQ==
--{0}
Content-Disposition: form-data; name=""__EVENTVALIDATION""

/wEWBAKXx6zsBALa1ffGCwK80PHQDQLurOv8AU7Jo8sYj9+E/zw7RsmFraAotTazyvQc7T2VseLqSwGO
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$fname""

jim
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$lname""

bob
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$picUpload""; filename=""C:\temp\small.JPG""
Content-Type: image/jpeg

", boundry);

        string endData = string.Format(@"
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$post""


--{0}--
", boundry);


        List<byte> postByteArray = Encoding.UTF8.GetBytes(postData).ToList();

        byte[] file = File.ReadAllBytes(@"C:\temp\small.JPG");

        postByteArray.AddRange(file);

        postByteArray.AddRange(Encoding.UTF8.GetBytes(endData));

        client.Headers.Add("Content-Type", contentType);

        client.UploadData("http://localhost:63247/Default.aspx", "POST", postByteArray.ToArray());
    }   

这篇关于C#发布到其他服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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