首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎样使用HttpWebRequest提交Post请求,发送文件?解决方法

2012-04-05 
怎样使用HttpWebRequest提交Post请求,发送文件?最近使用sina微博的api:https://api.weibo.com/2/statuses/

怎样使用HttpWebRequest提交Post请求,发送文件?
最近使用sina微博的api:https://api.weibo.com/2/statuses/upload.json时,需要使用post请求发送二进制图片,不知怎样解决?

C# code
string postData = "access_token=" + access_token;                    postData += "&status=" + Server.UrlEncode(GetWeather());postData += "&pic=";FileStream file = new FileStream("C:\\Documents and Settings\\zhengw.BAUERHOME\\桌面\\weather.png", FileMode.Open, FileAccess.Read);BinaryReader read = new BinaryReader(file);int count = (int)file.Length;                    byte[] buffer = new byte[count];                    read.Read(buffer, 0, buffer.Length);                    string msg = Encoding.UTF8.GetString(buffer);                    data = encoding.GetBytes(postData+msg);                    request = (HttpWebRequest)WebRequest.Create(url);                    request.Method = "POST";request.ContentType = "multipart/form-data";                    request.ContentLength = data.Length;

但在
C# code
response = (HttpWebResponse)request.GetResponse();
时捕捉到了异常,403
求解决办法!!!!

[解决办法]
WebClient w = new WebClient();
System.Collections.Specialized.NameValueCollection VarPost = new System.Collections.Specialized.NameValueCollection();
VarPost.Add("id", s_id);//将textBox1中的数据变为用a标识的参数,并用POST传值方式传给网页 ­

//将参数列表VarPost中的所有数据用POST传值的方式传给http://申请好的域名或用IIs配置好的地址/Default.aspx,

//并将从网页上返回的数据以字节流存放到byRemoteInfo中)(注:IIS配置的时候经常没配置好会提示错误,嘿) ­

w.UploadValues("http://10658.com.cn/api/106sms.php", "POST", VarPost);

热点排行