Forms 在Windows metro应用程序中使用多部分/表单数据
我正在尝试创建一种方法,通过多部分/表单数据HTTP POST请求将文本数据(数据库键)和.png图像从Windows 8.1应用程序上传到Web服务器上托管的PHP脚本(使用数据库键作为文件名在本地保存图像) 我已经看过包嗅探器了,它是按照我认为应该的方式出现的,但问题是PHP$\u POST值没有通过,并且在PHP脚本端为null 以下是Windows Metro应用程序端的C#代码:Forms 在Windows metro应用程序中使用多部分/表单数据,forms,http,post,Forms,Http,Post,我正在尝试创建一种方法,通过多部分/表单数据HTTP POST请求将文本数据(数据库键)和.png图像从Windows 8.1应用程序上传到Web服务器上托管的PHP脚本(使用数据库键作为文件名在本地保存图像) 我已经看过包嗅探器了,它是按照我认为应该的方式出现的,但问题是PHP$\u POST值没有通过,并且在PHP脚本端为null 以下是Windows Metro应用程序端的C#代码: private async void UploadImage() { strin
private async void UploadImage() {
string newLine = Environment.NewLine;
WebRequest request = WebRequest.Create("http://www.mywebsite.com/receiveimage.php");
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";
string boundary = "---------------------BUIUFBILPQMZ81C12CCC2EVV2RJ";
request.ContentType = "multipart/form-data; boundary=" + boundary;
// Open pre-saved MY_FILE.png file from local disk and store in fh
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile fh = await localFolder.GetFileAsync("MY_FILE.png");
// Header strings and values
string Prefix1 = boundary + newLine + "Content-Disposition: form-data; name=\"SERIAL_NO\"" + newLine + newLine; // SERIAL_NO is the php $_POST variable
string Prefix2 = boundary + newLine + "Content-Disposition: form-data; name=\"MY_FILE\"; filename=\"" + "MY_FILE.png\"" + newLine + "Content-Type: application/base64" + newLine + newLine;
byte[] baPrefix1 = System.Text.Encoding.UTF8.GetBytes(Prefix1);
byte[] baValue1 = System.Text.Encoding.UTF8.GetBytes("C4145-12" + newLine); // (C4145-12 is the database key)
IBuffer buffer = await FileIO.ReadBufferAsync(fh);
byte[] baPrefix2 = System.Text.Encoding.UTF8.GetBytes(Prefix1);
byte[] baValue2 = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(buffer.ToArray()).Replace("+", "%2B") + newLine);
byte[] combinedData = new byte[baPrefix1.Length + baValue1.Length + baPrefix2.Length + baValue2.Length];
System.Buffer.BlockCopy(baPrefix1, 0, combinedData, 0, baPrefix1.Length);
System.Buffer.BlockCopy(baValue1, 0, combinedData, baPrefix1.Length, baValue1.Length);
System.Buffer.BlockCopy(baPrefix2, 0, combinedData, baPrefix1.Length + baValue1.Length, baPrefix2.Length);
System.Buffer.BlockCopy(baValue2, 0, combinedData, baPrefix1.Length + baValue1.Length + baPrefix2.Length, baValue2.Length);
Stream dataStream = await request.GetRequestStreamAsync();
dataStream.Write(combinedData, 0, combinedData.Length);
dataStream.Dispose();
using (WebResponse response = await request.GetResponseAsync())
{
// "STATUS" is just a text block
STATUS.Text = (((HttpWebResponse)response).StatusDescription);
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseFromServer = reader.ReadToEnd();
STATUS.Text = responseFromServer;
}
}
服务器上的PHP脚本与其他表单一起工作,但无法识别从该脚本传入的值,因此我知道这不是问题。我确信这是标题格式的问题。我尝试了大量的新台词,但没有用。有什么想法吗?MIME部分由
“--”+boundary
分隔,最后一部分由“--”+boundary+“--”
终止。您缺少前导和终止的“--”
。另外,在分配baPrefix2
时,您再次使用Prefix1
而不是Prefix2
。另外,对于PNG,application/base64
不是有效的内容类型,您需要使用image/PNG
,并包含一个单独的内容传输编码:base64
头。但是许多Web服务器不处理表单数据POST上的base64,因此您应该删除base64并以8bit数据的形式发送文件数据。您所展示的不是一个好的MIME实现。是的,它被证明是丢失的前一个/后一个“-”,在丢弃它的边界旁边。现在可以了,谢谢!