解决方案:
好啊昨天晚上我一直在努力,取得了一些进展。我使用Fiddler来构造POST消息,以查看上面的内容与服务器期望的内容之间的任何差异。
我让它发送消息并返回HTTP 200 OK响应。同样,这段代码还没有准备好生产,只是需要测试一下,看看是否可以让Zeep正常工作。感谢所有回复的人,如果您正在寻找ZEEP代码,我希望这能对您有所帮助。
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Web;
using System.Web.Handlers;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApplication1
{
class Program
{
public static string API_KEY = "YOUR_API_KEY_GOES_HERE! INCLUDE DASHES!";
public static string SECRET_ACCESS_KEY = "YOUR_SECRET_KEY_GOES_HERE!";
static void Main(string[] args)
{
Console.WriteLine("BLAST - \r\n\r\n");
BlastTcpPost();
Console.WriteLine("SEND - \r\n\r\n");
SendTcpPost();
}
public static void BlastTcpPost()
{
SendSMS(
"https://api.zeepmobile.com/messaging/2008-07-14/blast_message",
"You are on blast",
string.Empty
);
}
public static void SendTcpPost()
{
SendSMS(
"https://api.zeepmobile.com/messaging/2008-07-14/send_message",
"You are a user...good job!",
"22"
);
}
public static string SendSMS(string requestUrl, string body, string user)
{
string parameters = "";
string requestHeaders = "";
string responseData = "";
string http_date = DateTime.UtcNow.ToString("r");
body = HttpUtility.UrlEncode(body, System.Text.Encoding.UTF8);
if (user.Length > 0) parameters += "user_id=" + user + "&";
if (body.Length > 0) parameters += "body=" + body;
string canonicalString = API_KEY + http_date + parameters;
HMACSHA1 hmacsha1 = new HMACSHA1(SECRET_ACCESS_KEY.ToByteArray());
byte[] hashValue = hmacsha1.ComputeHash(canonicalString.ToByteArray());
String b64Mac = hashValue.ToBase64String();
String authentication = String.Format("Zeep {0}:{1}", API_KEY, b64Mac);
string auth = String.Format("Zeep {0}:{1}", API_KEY, b64Mac);
System.Uri uri = new Uri(requestUrl);
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(uri.Host, uri.Port);
string requestMethod = "POST " + uri.LocalPath + " HTTP/1.1\r\n";
requestHeaders += "Host: api.zeepmobile.com\r\n";
requestHeaders += "Authorization: " + auth + "\r\n";
requestHeaders += "Date: " + DateTime.UtcNow.ToString("r") + "\r\n";
requestHeaders += "Content-Type: application/x-www-form-urlencoded\r\n";
requestHeaders += "Content-Length: " + parameters.ToByteArray().Length + "\r\n";
requestHeaders += "\r\n";
Byte[] data = System.Text.Encoding.UTF8.GetBytes(requestMethod + requestHeaders + parameters + "\r\n");
NetworkStream stream = client.GetStream();
System.Net.Security.SslStream sslStream = new System.Net.Security.SslStream(
stream,
false,
new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate));
sslStream.AuthenticateAsClient(uri.Host);
sslStream.Write(data, 0, data.Length);
sslStream.Flush();
for (int i = 0; i < 100; i++)
{
if (stream.DataAvailable)
{
break;
}
System.Threading.Thread.Sleep(100);
}
Byte[] bytes = new byte[1024];
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (stream.DataAvailable)
{
int count = sslStream.Read(bytes, 0, 1024);
if (count == 0)
{
break;
}
sb.Append(System.Text.Encoding.UTF8.GetString(bytes, 0, count));
}
responseData = sb.ToString();
Console.WriteLine(responseData);
client.Close();
return responseData;
}
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
}
public static class Extensions
{
public static byte[] ToByteArray(this string input)
{
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(input);
}
public static string ToBase64String(this byte[] input)
{
return Convert.ToBase64String(input);
}
}
}