怎样使用HttpWebRequest提交Post请求,发送文件?
最近使用sina微博的api:https://api.weibo.com/2/statuses/upload.json时,需要使用post请求发送二进制图片,不知怎样解决?
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;response = (HttpWebResponse)request.GetResponse();