C#中的Facebook上传问题

Facebook Upload Issue in C#
2021-03-07
  •  译文(汉语)
  •  原文(英语)

当我尝试上传images/videosfacebook使用facebook API.在facebook API给出例外如下图所示

"One or more errors occurred.System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Security._SslStream.StartWriting(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at Facebook.FacebookClient.<>c__DisplayClass6.<ApiAsync>b__4(Object o, OpenWriteCompletedEventArgs e) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)"

使用的代码是:

FacebookMediaStream facebookMediaObject = new FacebookMediaStream { FileName = fileName, ContentType = mimeType };
Stream stream = new MemoryStream(bytearray);
facebookMediaObject.SetValue(stream);                   

var postInfo = new Dictionary<string, object>();
postInfo.Add("message", fileName);
postInfo.Add("image", facebookMediaObject);
var tasktest = _fbClient.PostTaskAsync(String.Format("/{0}/photos", parentFolderID), postInfo);
tasktest.Wait();
facebookMediaObject.Dispose();
stream.Dispose();
速聊1:
也发布您的代码!
解决过程1

查看此Prabir的博客

var fb = new FacebookClient("access_token");
dynamic parameters = new ExpandoObject();
parameters.source = new FacebookMediaObject { ContentType = "video/3gpp", FileName = "video.3gp" }.SetValue(File.ReadAllBytes(@"c:\video.3gp"));
parameters.title = "video title";
parameters.description = "video description";

dynamic result = fb.Post("/me/videos", parameters);
Console.WriteLine(result);

When i try to upload images/videos to facebook using facebook API. The facebook API gives exception shown as below

"One or more errors occurred.System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Security._SslStream.StartWriting(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at Facebook.FacebookClient.<>c__DisplayClass6.<ApiAsync>b__4(Object o, OpenWriteCompletedEventArgs e) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)"

The code used is :

FacebookMediaStream facebookMediaObject = new FacebookMediaStream { FileName = fileName, ContentType = mimeType };
Stream stream = new MemoryStream(bytearray);
facebookMediaObject.SetValue(stream);                   

var postInfo = new Dictionary<string, object>();
postInfo.Add("message", fileName);
postInfo.Add("image", facebookMediaObject);
var tasktest = _fbClient.PostTaskAsync(String.Format("/{0}/photos", parentFolderID), postInfo);
tasktest.Wait();
facebookMediaObject.Dispose();
stream.Dispose();
Talk1:
post your code as well!
Solutions1

Check this Prabir's Blog

var fb = new FacebookClient("access_token");
dynamic parameters = new ExpandoObject();
parameters.source = new FacebookMediaObject { ContentType = "video/3gpp", FileName = "video.3gp" }.SetValue(File.ReadAllBytes(@"c:\video.3gp"));
parameters.title = "video title";
parameters.description = "video description";

dynamic result = fb.Post("/me/videos", parameters);
Console.WriteLine(result);
转载于:https://stackoverflow.com/questions/23003562/facebook-upload-issue-in-c-sharp

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入