c# post上传文件
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Web;
using System.Net;
namespace policesend
{
public partial class Form1 : Form
{
public string guestPath = "", photoPaht = "";
public Form1()
{
InitializeComponent();
}
public class TestUpload
{
public static void main(string strURL)
{
Stream instream = null;
Stream requestStream = null;
HttpWebResponse response = null;
HttpWebRequest req = null;
StreamReader sr = null;
FileStream m_guest = null;
FileStream m_photo = null;
try
{
string guestPath = @"c:\a.txt";
string photoPaht = @"c:\b.jpg";
m_guest = new FileStream(guestPath, FileMode.Open,FileAccess.Read);
m_photo = new FileStream(photoPaht, FileMode.Open,FileAccess.Read);
//身份信息文本文件上传HTTP请求头
string BOUNDARY = "---------------------------7d"+getString(11);
StringBuilder m_sbGuest = new StringBuilder();
m_sbGuest = m_sbGuest.Append("--");
m_sbGuest = m_sbGuest.Append(BOUNDARY);
m_sbGuest = m_sbGuest.Append("\r\n");
m_sbGuest = m_sbGuest.Append("Content-Disposition:form-data;name=\"" + "guest" + "\";filename=\"" + guestPath + "\"\r\n");
m_sbGuest = m_sbGuest.Append("Content-Type:text/plain\r\n\r\n");
byte[] start_data_guest = Encoding.ASCII.GetBytes(m_sbGuest.ToString());
//照片上传http请求头
StringBuilder m_sbPhoto = new StringBuilder();
m_sbPhoto = m_sbPhoto.Append("--");
m_sbPhoto = m_sbPhoto.Append(BOUNDARY);
m_sbPhoto = m_sbPhoto.Append("\r\n");
m_sbPhoto = m_sbPhoto.Append("Content-Disposition:form-data;name=\"" + "photo" + "\";filename=\"" + photoPaht + "\"\r\n");
m_sbPhoto = m_sbPhoto.Append("Content-Type:image/jpg\r\n\r\n");
byte[] start_data_photo = Encoding.ASCII.GetBytes(m_sbPhoto.ToString());
//http请求结尾
byte[] end_data = Encoding.ASCII.GetBytes("\r\n--" + BOUNDARY + "--\r\n");
string str;
str=m_sbGuest.ToString() + m_sbPhoto.ToString() + "\r\n--" + BOUNDARY + "--\r\n";
MessageBox.Show(str);
//建立HTTP链接
string URL = null;
URL = "http://61.163.108.195:9080/getguestfile/jsp/testupload.jsp";//上传文件处理服务地址
req = (HttpWebRequest)WebRequest.Create(URL);
req.Method = "POST";
req.Referer=
req.ContentType = "multipart/form-data;boundary=" + BOUNDARY;
req.KeepAlive = true;
req.ContentLength = start_data_guest.Length + start_data_photo.Length + getFile(m_guest).Length + getFile(m_photo).Length + end_data.Length * 2;
requestStream = req.GetRequestStream();
requestStream.Write(start_data_guest, 0, start_data_guest.Length);
requestStream.Write(getFile(m_guest), 0, getFile(m_guest).Length);
requestStream.Write(end_data, 0, end_data.Length);
requestStream.Write(start_data_photo, 0, start_data_photo.Length);
requestStream.Write(getFile(m_photo), 0, getFile(m_photo).Length);
requestStream.Write(end_data, 0, end_data.Length);
//requestStream.Flush();
requestStream.Close();
response = req.GetResponse() as HttpWebResponse;
if (response.StatusCode.ToString() == "OK")
{
instream = response.GetResponseStream();
sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr.ReadToEnd();
//MessageBox.Show(backstr);
}
else
{
MessageBox.Show("链接失败:" + response.StatusCode.ToString());
}
sr.Close();
response.Close();
m_guest.Close();
m_photo.Close();
//return true;
}
catch (Exception ex)
{
m_guest.Close();
m_photo.Close();
MessageBox.Show(ex.Message.ToString());
//return false;
}
}
public static byte[] getFile(FileStream m_fs)
{
byte[] pReadByte = new byte[0];
BinaryReader r = new BinaryReader(m_fs);
r.BaseStream.Seek(0, SeekOrigin.Begin);
pReadByte = r.ReadBytes((int)r.BaseStream.Length);
return pReadByte;
}
}
//将字节数组转换成图片
public static Image ByteToImage(byte[] bytImage)
{
Image img = null;
ImageConverter imgCvt = new ImageConverter();
object obj = imgCvt.ConvertFrom(bytImage);
img = (Image)obj;
return img;
}
public static string getString(int count)
{
int number;
string checkCode = String.Empty; //存放随机码的字符串
System.Random random = new Random();
for (int i = 0; i < count; i++) //产生4位校验码
{
number = random.Next();
number = number % 36;
if (number < 10)
{
number += 48; //数字0-9编码在48-57
}
else
{
number += 55; //字母A-Z编码在65-90
}
checkCode += ((char)number).ToString();
}
return checkCode;
}
private void button1_Click(object sender, EventArgs e)
{
TestUpload.main(textBox1.Text);
}
}
}
上面的代码为什么上传不上文件呢? 请高手指点! --------------------编程问答-------------------- 我觉得上传文件不是post.
.ftp可以.. --------------------编程问答-------------------- 帮你顶 --------------------编程问答-------------------- public string HttpUploadFile(string url, string poststr,string fileformname, string filepath, string cookie, string refre)
{
string boundary = "—————————7d930d1a850658";
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
webrequest.Headers.Add("Cookie: " + cookie);
webrequest.Referer = refre;
StringBuilder sb = new StringBuilder();
foreach (string c in poststr.Split(‘&’))
{
string[] item = c.Split(‘=’);
if (item.Length!=2)
{
break;
}
string name = item[0];
string value = item[1];
sb.Append("–" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"" + name + "\"");
sb.Append("\r\n\r\n");
sb.Append(value);
sb.Append("\r\n");
}
sb.Append("–" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"icon\";filename=\"" + filepath + "\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("image/pjpeg");
sb.Append("\r\n\r\n");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n–" + boundary + "–\r\n");
FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
WebResponse responce = webrequest.GetResponse();
Stream s = responce.GetResponseStream();
StreamReader sr = new StreamReader(s);
return sr.ReadToEnd();
}
补充:.NET技术 , C#