|
1金钱
后台有个 unit_id的属性 我这边要post一个unit_id的值过去,我下面这样写对不对 得到的返回值是 undefined index:unit_id。。。。。。。。
是我的问题还是后台的问题
String fileToUpload = "f://testDown.gif";
String uploadUrl = "http://cloud.ibinfen.com/app/index.php ";
String fileFormName = "file";
String contenttype = "image/gif";
string unitId = "unit_id=1";
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(fileFormName);
sb.Append("\"; filename=\"");
sb.Append(System.IO.Path.GetFileName(fileToUpload));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
byte[] uintBytes = Encoding.ASCII.GetBytes(unitId);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + uintBytes.Length + boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
MessageBox.Show(webrequest.Headers.ToString());
byte[] buffer = new Byte[(int)fileStream.Length];
int bytesRead = 0;
// while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
// requestStream.Write(buffer, 0, bytesRead);
requestStream.Write(uintBytes, 0, uintBytes.Length);
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
requestStream.Close();
WebResponse webRespon = webrequest.GetResponse();
Stream s = webRespon.GetResponseStream();
StreamReader sr = new StreamReader(s);
//读取服务器端返回的消息
String sReturnString = sr.ReadToEnd();
MessageBox.Show(sReturnString);
|
|