今天我有一个程序可以张贴到我的网站, 但我需要将文件上传支持添加到我的程序( 用于jpg ) 。 我有一个这样的问题, 只是让我的程序发布, 而现在经过许多小时后, 我无法让它与文件上传一起工作 。
Today my code is not very good but it works and I m the only one using it. Here is my code, abit stripped but I hope you get the idea what I need help with. 谢谢!
(“ 更新文件” 是图像的输入字段名称)
public static string Post(string url, int id, string message)
{
try
{
string param =
string.Format(
"MAX_FILE_SIZE=2097152&id={0}&com={1}&upfile", id, message);
byte[] bytes = Encoding.ASCII.GetBytes(param);
var post = (HttpWebRequest)WebRequest.Create(url);
post.ProtocolVersion = HttpVersion.Version10;
post.Method = "POST";
post.AllowAutoRedirect = false;
post.ContentType = "application/x-www-form-urlencoded";
post.ContentLength = bytes.Length;
post.UserAgent =
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 (.NET CLR 3.5.30729)";
post.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
Stream requestStream = post.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
var webResponse = (HttpWebResponse)post.GetResponse();
var sr = new StreamReader(webResponse.GetResponseStream());
return sr.ReadToEnd();
}
catch (Exception exception)
{
return null;
}
}
Edit: Sorry for my poor explanation here is what I need help with: I use the method above today to post on my site, but I need add support to upload files in my method. I can t change how the webpage works in the close future so it have to be done by using a POST request. I can t get it to work then I try to do it myself, so I m wondering if anybody could help me with what need to be added to read a image and post it with the request!
谢谢!