首页 新闻 会员 周边 捐助

webrequest post 文件

0
悬赏园豆:50 [已解决问题] 解决于 2010-03-14 10:50

最近几天一直在弄这个东西, 向别的网站 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"); 这样产生的并不相同, 是不是跟这个也有关系 ?

郁闷坏了,   多多指教  ,在此拜谢

 

蓝蓝的天的主页 蓝蓝的天 | 初学一级 | 园豆:50
提问于:2010-03-10 17:54
< >
分享
最佳答案
0

确定下这个是否有用户登陆限制,如果有用户登陆限制,那你需要先模拟登陆,然后使用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())
{
   //在这里进行图片上传

}

基本思路如上。

注意如果有登陆验证的话,请在执行这段代码前面先做模拟登陆,操作方式和发送参数这段代码类似。

收获园豆:25
西越泽 | 专家六级 |园豆:10775 | 2010-03-11 15:20
登陆的部分已经没问题了, 取到了cookie 的,否则传的时候回返回需要登陆的信息, 关键还是在传文件这里
蓝蓝的天 | 园豆:50 (初学一级) | 2010-03-11 18:07
其实就是 用webrequest 向博客中的相册传图片
蓝蓝的天 | 园豆:50 (初学一级) | 2010-03-12 11:14
其他回答(1)
0

没有那么麻烦,参考利用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文件逻辑

收获园豆:25
查尔斯 | 园豆:3832 (老鸟四级) | 2010-03-10 18:05
因为我还要传一些cookie 和一些参数过去, WebClient 怎么传啊 ?
支持(0) 反对(0) 蓝蓝的天 | 园豆:50 (初学一级) | 2010-03-10 22:26
System.Net.WebClient wc = new System.Net.WebClient(); wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"); wc.Headers.Headers.Add("Accept", "*/*)"); String cookie = wc.ResponseHeaders["Set-Cookie"]; wc.Headers.Add("Cookie", cookie); string uriString = "http://www.mysite.com"; byte[] responseArray = wc.UploadData(uriString,"POST",myNameValueCollection);
支持(0) 反对(0) 查尔斯 | 园豆:3832 (老鸟四级) | 2010-03-11 02:53
这个是 cookie 其它的参数 怎么传呢
支持(0) 反对(0) 蓝蓝的天 | 园豆:50 (初学一级) | 2010-03-11 08:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册