最近几天一直在弄这个东西, 向别的网站 post 文件(图片)
也查了不少资料, 可是怎么也传不上去, 开始提示 文件分析错误, 后来根据网上的一些东西 修改后 返回没没错误,但是文件还是没成功 .
难道是做了限制了吗?
怎么才能知道 是否限制 还是我程序有问题
List<Stream> fileStream = new List<Stream>();
// Stream sm = System.Net.WebRequest.Create(imgurl).GetResponse().GetResponseStream();
FileStream sm = new FileStream(Server.MapPath("..") + "\\OtherPost1.jpg", FileMode.Open, FileAccess.Read, FileShare.None);
fileStream.Add(sm);
string boundary = "-----------------------------BOUNDARY";// "----------" + DateTime.Now.Ticks.ToString("x");
GetFocusCookies();
Uri url = new Uri(url);
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Referer = url;
webrequest.Method = "Post";
webrequest.CookieContainer = cookie;
webrequest.ContentType = "multipart/form-data; boundary=--" + boundary;
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("file[0]");
sb.Append("\"; filename=\"");
sb.Append(Server.MapPath("..") + "\\OtherPost1.jpg");
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("image/pjpeg");
sb.Append("\r\n");
sb.Append("\r\n");
StringBuilder sb1 = new StringBuilder();
sb1.Append(Formatrequest(boundary, "border", "0"));
sb1.Append(Formatrequest(boundary, "alt", ""));
sb1.Append(Formatrequest(boundary, "file[0]", "filename=\"" + Server.MapPath("..") + "\\OtherPost1.jpg" + "\" Content-Type: image/jpg"));
sb1.Append(Formatrequest(boundary, "horiz", ""));
sb1.Append(Formatrequest(boundary, "isImg2PP", "1"));
sb1.Append(Formatrequest(boundary, "lurl", ""));
sb1.Append(Formatrequest(boundary, "vert", ""));
sb1.Append(Formatrequest(boundary, "watermark", ""));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString());
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
byte[] postDate = Encoding.UTF8.GetBytes(sb1.ToString());
long length = postHeaderBytes.Length * fileStream.Count + fileStream.Sum(s1 => s1.Length)
+ (boundaryBytes.Length * fileStream.Count) + postDate.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postDate, 0, postDate.Length);
foreach (var filestream1 in fileStream)
{
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)filestream1.Length))];
int bytesRead = 0;
while ((bytesRead = filestream1.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
}
webrequest.Timeout = 1000000;
WebResponse responce = webrequest.GetResponse();
Stream s = responce.GetResponseStream();
StreamReader sr = new StreamReader(s);
string str = sr.ReadToEnd();
sm.Close();
sm.Dispose();
还有个问题就是 string boundary 这个地方的值 , 网上很多都是根据时间生成的, 我通过IE监视 ,发现它的跟 DateTime.Now.Ticks.ToString("x"); 这样产生的并不相同, 是不是跟这个也有关系 ?
郁闷坏了, 多多指教 ,在此拜谢
确定下这个是否有用户登陆限制,如果有用户登陆限制,那你需要先模拟登陆,然后使用webrequest.CookieContainer把Cookie保存起来备用,待通过登陆验证后实行上传,上传过程确定是否有额外参数需要发送到服务器上,如果你参数使用GET的方式提交,参数可以直接添加到url后面(比如你需要提交的地址是http://www.cnblogs.com/upload.aspx提交参数为a=123,b=321,此时组合后的url是http://www.cnblogs.com/upload.aspx?a=123&b=321).
这里如果使用POST来提交参数的话,稍微麻烦些。可以用如下代码实现
string param = "a=123&b=321";
byte[] bs = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.cnblogs.com/upload.aspx" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
待参数发送结束后然后后面进行图片上传
using (WebResponse wr = req.GetResponse())
{
//在这里进行图片上传
}
基本思路如上。
注意如果有登陆验证的话,请在执行这段代码前面先做模拟登陆,操作方式和发送参数这段代码类似。
没有那么麻烦,参考利用Webclient将本地图片上传到服务器指定地址
string strRequest = "www.test.com/test.php";
string strFile = "本地图片路径";
WebClient oWebClient = new WebClient();
FileStream oStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
BinaryReader oReader = new BinaryReader(oStream);
Byte[] postArray = oReader.ReadBytes(Convert.ToInt32(oStream.Length));
Stream postStream = oWebClient.OpenWrite(strRequest, "POST");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Close();
另外检查你服务器端的接受post文件逻辑