您的位置:首页 > 理论基础 > 计算机网络

C# 使用HttpWebRequest Post

2014-02-19 15:53 309 查看
1.代码

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] data=Encoding.ASCII.GetBytes(String.Format("id={0}&balance={1}",id,balance));
            request.ContentLength = data.Length;
            request.GetRequestStream().Write(data, 0, data.Length);
            request.GetRequestStream().Flush();
            request.GetRequestStream().Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr =new StreamReader( response.GetResponseStream() );
2.注意

有因为POST的数据格式有xml,json,form等格式,为了让服务器后台明确数据的格式,最好指明ContentType,否则可能出现服务器接受不到参数

3.附:

form表单格式:application/x-www-form-urlencoded

json格式:json/application

xml格式:text/xml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: