哪位高人帮忙看一下呀,怎么始终传不上去文件呀

xushre 2013-07-21 02:29:37
以下是WP8 C#实现源码

void pct_Completed(object sender, PhotoResult e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.TaskResult.Equals(TaskResult.OK))
{
UploadStateObject uso = new UploadStateObject();
uso.Stream = e.ChosenPhoto;
uso.Boundary = "----WebKitFormBoundary3jJRGWjdL0V3CTvW";
uso.OriginalFileName = e.OriginalFileName;

HttpWebRequest request = HttpWebRequest.CreateHttp(new Uri(String.Format(liveUpload, id), UriKind.Absolute));
request.Method = "POST";
request.CookieContainer = cc;
request.ContentType = "multipart/form-data; boundary=" + uso.Boundary;
request.AllowAutoRedirect = true;

uso.Request = request;

request.BeginGetRequestStream(GetRequestStream, uso);
pi.IsVisible = true;
}
}

private void GetRequestStream(IAsyncResult ar)
{
UploadStateObject uso = ar.AsyncState as UploadStateObject;

Stream bw = uso.Request.EndGetRequestStream(ar);

String[] fileName = uso.OriginalFileName.Split(new char[] { '\\' });

Byte[] boundary = Encoding.UTF8.GetBytes(uso.Boundary + "\r\n");
Byte[] disposition = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"file\"; filename=\"wp.jpg\"\r\n");
Byte[] contentType = Encoding.UTF8.GetBytes("Content-Type: image/jpeg\r\n\r\n");
Byte[] endBoundary = Encoding.UTF8.GetBytes("\r\n" + uso.Boundary + "--");




Byte[] ff = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"localUrl\"");
Byte[] va = Encoding.UTF8.GetBytes("\r\n\r\nwp.jpg\r\n");
bw.Write(boundary, 0, boundary.Length);
bw.Write(ff, 0, ff.Length);
bw.Write(va, 0, va.Length);


bw.Write(boundary, 0, boundary.Length);
bw.Write(disposition, 0, disposition.Length);
bw.Write(contentType, 0, contentType.Length);

Byte[] buffer = new Byte[2048];
Int32 len = 0;
while ((len = uso.Stream.Read(buffer, 0, buffer.Length)) != 0)
{
bw.Write(buffer, 0, len);
}
bw.Write(endBoundary, 0, endBoundary.Length);

bw.Close();
uso.Stream.Close();

this.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("发送完成");
});

uso.Request.BeginGetResponse(GetResponse, uso);
}

private void GetResponse(IAsyncResult ar)
{
UploadStateObject uso = ar.AsyncState as UploadStateObject;
HttpWebResponse response = (HttpWebResponse)uso.Request.EndGetResponse(ar);
}

class UploadStateObject
{
public HttpWebRequest Request { get; set; }
public Stream Stream { get; set; }
public String Boundary { get; set; }
public String OriginalFileName { get; set; }
}


Fiddler2监视的结果
请求Header

请求正文

服务器返回结果

服务器端是PHP开发的, 三个array分别是print_r($_REQUEST); print_r($_POST); print_r($_FILES);
C#端没有报错,而且Cookies也没有传上去,不知道是不是哪儿写错了,请高手看看。
...全文
177 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
xushre 2013-07-22
  • 打赏
  • 举报
回复
搞了半天是Byte[] boundary = Encoding.UTF8.GetBytes(uso.Boundary + "\r\n");少加了两个-

7,657

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧