[英]Facebook chat PLAIN authentication issue Windows Phone
I'am trying to write simple library for facebook chat, but I've encountered an issue at the very beginning. 我正在尝试为Facebook聊天编写简单的库,但是一开始我就遇到了一个问题。 I do not know why chat.facebook.com sends me "<" after sending auth element.
我不知道为什么chat.facebook.com在发送auth元素后向我发送“ <”。 Having searched through the Internet I found nothing that colud help me.
通过互联网搜索后,我发现没有什么可以帮助我的。 Related code is at a very end of following code block.
相关代码在以下代码块的最后。
public const string BEGIN_AUTH_PLAIN = @"<auth xmlns='urn:ietf:params:xml:ns:xmpp- sasl' mechanism='PLAIN'>";
public const string END_AUTH = @"</auth>";
public static string GetPlainAuth(string email, string password)
{
byte[] plain = System.Text.Encoding.UTF8.GetBytes((char)0+email+(char)0+password);
string credentials = System.Convert.ToBase64String(plain);
return String.Format("{0}{1}{2}", BEGIN_AUTH_PLAIN, credentials, END_AUTH);
}
//////////////////////////////////////////////////////////////////////////////
public async Task<bool> NegotiateStream()
{
if (!connected)
return false;
writer.WriteString(XmlConstants.BEGIN_STREAM);
await writer.StoreAsync();
await reader.LoadAsync(4096);
string resp = reader.ReadString(reader.UnconsumedBufferLength);
//check if response is stream and offers plaintext mechanism
if (!resp.Contains("stream:stream") || !resp.Contains("<mechanism>PLAIN"))
return false;
writer.WriteString(XmlConstants.START_TLS);
await writer.StoreAsync();
await reader.LoadAsync(4096);
resp = reader.ReadString(reader.UnconsumedBufferLength);
if (!resp.Contains("proceed"))
return false;
//now upgrate to tls connection
await socket.UpgradeToSslAsync(SocketProtectionLevel.Ssl, HostName);
//now try to login
writer.WriteString(XmlConstants.BEGIN_STREAM);
await writer.StoreAsync();
await reader.LoadAsync(4096);
resp = reader.ReadString(reader.UnconsumedBufferLength);
if (!resp.Contains("stream:stream") || !resp.Contains("<mechanism>PLAIN"))
return false;
//now send auth
string xx = XmlConstants.GetPlainAuth(Email, Password);
writer.WriteString(xx);
await writer.StoreAsync();
await reader.LoadAsync(4096);
resp = reader.ReadString(reader.UnconsumedBufferLength);
//could not login
if (!resp.Contains("success"))
return false;
return true;
}
I had to do a hack like this(do/while). 我不得不做这样的黑客(做/同时)。 But I would recommend you to use a library.
但是我建议您使用一个库。 Here is as far as I got trying to convert the php example into C#.
这是我尝试将php示例转换为C#的范围。 http://pastebin.com/jst7qkrh Here is the thread on SO: Send message via Facebook Chat API (XMPP) C#
http://pastebin.com/jst7qkrh这是SO上的线程: 通过Facebook Chat API(XMPP)C#发送消息
public string SendWihSsl(string dataToSend)
{
Byte[] data = System.Text.Encoding.UTF8.GetBytes(dataToSend);
ssl.Write(data, 0, data.Length);
ssl.Flush();
data = new Byte[2048];
int myBytesRead = 0;
StringBuilder myResponseAsSB = new StringBuilder();
do
{
myBytesRead = ssl.Read(data, 0, data.Length);
myResponseAsSB.Append(System.Text.Encoding.UTF8.GetString(data, 0, myBytesRead));
if (myResponseAsSB.ToString().IndexOf("</") != -1)
{
break;
}
} while (myBytesRead != 0);
return myResponseAsSB.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.