典型的HTTP POST code? [英] Canonical HTTP POST code?

查看:211
本文介绍了典型的HTTP POST code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过发送一个HTTP POST这么多的实施,诚然我不完全理解的底层细节就知道什么是必需的。

I've seen so many implementations of sending an http post, and admittedly I don't fully understand the underlying details to know what's required.

什么是简洁/正确/规范code到C#.NET发送一个HTTP POST 3.5?

我要像

public string SendPost(string url, string data)

这可以被添加到库,并始终用于发布数据和将返回服务器响应。

that can be added to a library and always used for posting data and will return the server response.

推荐答案

我相信这个简单的版本将是

I believe that the simple version of this would be

var client = new WebClient();
return client.UploadString(url, data);

System.Net.WebClient 类有让你下载或上传字符串或文件,或字节其他有用的方法。

The System.Net.WebClient class has other useful methods that let you download or upload strings or a file, or bytes.

不幸的是(经常)的情况下,你必须做更多的工作。你需要进行身份验证代理服务器(尽管它会使用默认的代理配置IE)以上的例子并没有照顾的情况。

Unfortunately there are (quite often) situations where you have to do more work. The above for example doesn't take care of situations where you need to authenticate against a proxy server (although it will use the default proxy configuration for IE).

另外,Web客户端不支持多个文件或设置(某些特定)的头,有时你不得不去深入和使用

Also the WebClient doesn't support uploading of multiple files or setting (some specific) headers and sometimes you will have to go deeper and use the

System.Web.HttpWebRequest System.Net.HttpWebResponse 代替。

这篇关于典型的HTTP POST code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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