|
 |
|
สอบถามการเขียน POST โดยใช้ HttpWebRequest ใน CodeBehind หน่อยครับ |
|
 |
|
|
 |
 |
|
ไฟล์ไม่น่าจะส่งได้ครับ เพราะถ้าส่งได้ก็จะต้องส่งไปเป็นแบบ Binary ครับ
|
 |
 |
 |
 |
Date :
2011-06-13 14:33:31 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่อผมส่งได้แล้วครับ ขอบคุณ คุณวินมากครับ ^^
เพื่อนๆคนไหนสนใจนำไปประยุกต์ได้นะครับ
Code (ASP)
protected string ExecutePostCommand(string filename, byte[] photo, string text, string place)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://testdomain.com/api/post";);
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;
string boundary = System.Guid.NewGuid().ToString();
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
{
request.Credentials = new NetworkCredential(Username, Password);
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.Method = "POST";
// Build Contents for Post
string header = string.Format("--{0}", boundary);
string footer = header + "--";
StringBuilder contents = new StringBuilder();
// Image
contents.AppendLine(header);
contents.AppendLine(string.Format("Content-Disposition: form-data; name=\"image\"; filename=\"{0}\"", filename));
contents.AppendLine("Content-Type: image/jpeg");
contents.AppendLine();
contents.AppendLine(Encoding.UTF8.GetString(photo));
// Text
contents.AppendLine(header);
contents.AppendLine("Content-Disposition: form-data; name=\"text\"");
contents.AppendLine();
contents.AppendLine(text);
// Place
contents.AppendLine(header);
contents.AppendLine("Content-Disposition: form-data; name=\"place\"");
contents.AppendLine();
contents.AppendLine(place);
// Footer
contents.AppendLine(footer);
// This is sent to the Post
byte[] bytes = Encoding.UTF8.GetBytes(contents.ToString());
request.ContentLength = bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
using (WebResponse response = request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
}
}
return null;
}
Credit : http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.languages.csharp/2008-02/msg03264.html
|
ประวัติการแก้ไข 2011-06-13 16:39:44 2011-06-13 16:41:14
 |
 |
 |
 |
Date :
2011-06-13 15:53:56 |
By :
LunaziA |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แจ่ม
|
 |
 |
 |
 |
Date :
2011-06-15 05:32:26 |
By :
ee36 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|