- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
大家好。
我正在编写一些代码以通过 Zeep Mobile ( http://zeepmobile.com/developers/) 发送/接收 SMS 消息。
我查看了他们的谷歌群组,甚至联系了他们的支持人员,但他们不太擅长回复,我现在真的是瞎了眼。
我必须与他们整合(工作需要),但我不确定为什么我的代码无法正常工作。所以,我想知道是否有人愿意分享任何 C# .Net 代码,以便我可以将其集成到我的应用程序中。
当然,这完全取决于您是否有使用 Zeep 的经验。如果你想让我发布我的代码,我也可以这样做。让我知道。
谢谢,非常感谢您的帮助。
**
**
请原谅草率的代码。它只是我用来测试 Zeep 的东西,我希望有人能试一试。 (.Net 3.5 控制台应用程序,以防您想要构建它)。
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 = "MY_API_KEY";
public static string SECRET_ACCESS_KEY = "MY_SECRET_KEY";
public static string PATTERN_RFC1123 = "ddd, dd MMM yyyy HH:mm:ss " + "GMT";
static void Main(string[] args)
{
// URL for sending message -
// send_message = "https://api.zeepmobile.com/messaging/2008-07-14/send_message";
// blast_message = "https://api.zeepmobile.com/messaging/2008-07-14/blast_message";
string apiurl = "https://api.zeepmobile.com/messaging/2008-07-14/blast_message";
// FORMAT must be Sun, 06 Nov 1994 08:49:37 GMT
string http_date = DateTime.UtcNow.ToString("r");
// Text to send
string body = HttpUtility.UrlEncode("Test message.", System.Text.Encoding.UTF8);
// NOTE: Use 'user_id=22&body=' instead of just 'body=' when sending a message to a user.
// 22 is a user I have previously registered with ZEEP and is used for testing purposes.
string parameters = "body=" + body;
// String that will be converted into a signature.
string canonicalString = API_KEY + http_date + parameters;
//------------START HASH COMPUTATION---------------------
// Compute the Base64 HMACSHA1 value
HMACSHA1 hmacsha1 = new HMACSHA1(SECRET_ACCESS_KEY.ToByteArray());
// Compute the hash of the input file.
byte[] hashValue = hmacsha1.ComputeHash(canonicalString.ToByteArray());
String b64Mac = hashValue.ToBase64String();
String authentication = String.Format("Zeep {0}:{1}", API_KEY, b64Mac);
//-----------END HASH COMPUTATION------------------------
// We are using TCPClient instead of an HTTPWebRequest because we need to manually
// set the "Headers" such as Date, Authorization etc which cannot easily be done with HTTPWebRequest.
Uri reqUrl = new Uri(apiurl);
TcpClient client = new TcpClient(reqUrl.Host, reqUrl.Port);
NetworkStream netStream = client.GetStream();
// SSLStream is used for secure communication. ZEEP requires the use of SSL to send and SMS.
System.Net.Security.SslStream sslStream = new System.Net.Security.SslStream(
netStream,
false,
new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate));
sslStream.AuthenticateAsClient(reqUrl.Host);
// POST content we are going to transmit over the SSL channel.
// See. http://zeepmobile.com/developers/documentation/messaging/2008-07-14/rest_api#send_message
System.IO.StreamWriter s = new System.IO.StreamWriter(sslStream);
s.WriteLine(String.Format("POST {0} HTTP/1.1", "/api/blast"));
s.WriteLine(String.Format("Host: {0}", "api.zeepmobile.com"));
s.WriteLine(String.Format("Authorization: Zeep {0}:{1}", API_KEY, b64Mac));
s.WriteLine(String.Format("Date: {0}", http_date));
s.WriteLine(String.Format("Content-Type: {0}", "application/x-www-form-urlencoded"));
s.WriteLine(String.Format("Content-Length: {0}", parameters.Length));
s.WriteLine(String.Format("{0}", parameters));
s.Flush();
System.IO.StreamReader r = new StreamReader(sslStream);
string resp = r.ReadToEnd();
Console.WriteLine(resp);
r.Close();
}
// The following method is invoked by the RemoteCertificateValidationDelegate.
// We want to make sure the SSL has no Policy errors and is safe.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
}
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);
}
}
}
运行此代码时发生的错误如下图所示。
最佳答案
好的。我昨晚一直在努力,取得了一些进展。我使用 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();
}
/// <summary>
/// Send a BLAST to all users in your ZEEP account.
/// </summary>
public static void BlastTcpPost()
{
SendSMS(
"https://api.zeepmobile.com/messaging/2008-07-14/blast_message", // URL for Send_Message
"You are on blast", // Message to send
string.Empty // No UserId to send.
);
}
/// <summary>
/// Send a single message to a user in your ZEEP account.
/// </summary>
public static void SendTcpPost()
{
// Note:- 22 I use for the UserId is just a user I have signed up. Yours may be different and you
// might want to pass that in as a parameter.
SendSMS(
"https://api.zeepmobile.com/messaging/2008-07-14/send_message", // URL for Send_Message
"You are a user...good job!", // Message to send
"22" // User Id in your system.
);
}
/// <summary>
/// Uses a TCPClient and SSLStream to perform a POST.
/// </summary>
/// <param name="requestUrl">URL that the POST must be directed to.</param>
/// <param name="body">Message that is to be sent.</param>
/// <param name="user">UserId in your Zeep System. Only required if your sending a Single Message to a User.
/// Otherwise, just send a string.Empty.</param>
/// <returns>Response from the server. (although it will write the response to console)</returns>
public static string SendSMS(string requestUrl, string body, string user)
{
string parameters = "";
string requestHeaders = "";
string responseData = "";
// FORMAT must be Sun, 06 Nov 1994 08:49:37 GMT
string http_date = DateTime.UtcNow.ToString("r");
// Clean the text to send
body = HttpUtility.UrlEncode(body, System.Text.Encoding.UTF8);
if (user.Length > 0) parameters += "user_id=" + user + "&";
if (body.Length > 0) parameters += "body=" + body;
// String that will be converted into a signature.
string canonicalString = API_KEY + http_date + parameters;
//------------START HASH COMPUTATION---------------------
// Compute the Base64 HMACSHA1 value
HMACSHA1 hmacsha1 = new HMACSHA1(SECRET_ACCESS_KEY.ToByteArray());
// Compute the hash of the input file.
byte[] hashValue = hmacsha1.ComputeHash(canonicalString.ToByteArray());
String b64Mac = hashValue.ToBase64String();
String authentication = String.Format("Zeep {0}:{1}", API_KEY, b64Mac);
//-----------END HASH COMPUTATION------------------------
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";
// Set Headers for the POST message
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";
// Get the data to be sent as a byte array.
Byte[] data = System.Text.Encoding.UTF8.GetBytes(requestMethod + requestHeaders + parameters + "\r\n");
// Send the message to the connected TcpServer.
NetworkStream stream = client.GetStream();
// SSL Authentication is used because the Server requires https.
System.Net.Security.SslStream sslStream = new System.Net.Security.SslStream(
stream,
false,
new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate));
sslStream.AuthenticateAsClient(uri.Host);
// Send the data over the SSL stream.
sslStream.Write(data, 0, data.Length);
sslStream.Flush();
// Receive the TcpServer.response.
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);
// Close everything.
client.Close();
return responseData;
}
// The following method is invoked by the RemoteCertificateValidationDelegate.
// We want to make sure the SSL has no Policy errors and is safe.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
// Somehow the cert always has PolicyErrors so I am returning true regardless.
return true;
//if (sslPolicyErrors == SslPolicyErrors.None)
// return true;
//Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
//// Do not allow this client to communicate with unauthenticated servers.
//return false;
}
}
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);
}
}
}
关于c# - 短信的 .NET 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/526898/
创建使用.NET框架的asp.net页面时,访问该页面的客户端是否需要在其计算机上安装.NET框架? IE。用户访问www.fakesite.com/default.aspx,如果他们没有安装框架,他
我阅读了很多不同的博客和 StackOverflow 问题,试图找到我的问题的答案,但最后我找不到任何东西,所以我想自己问这个问题。 我正在构建一个应用程序,其中有一个长时间运行的工作线程,它执行一些
已锁定。这个问题及其答案是locked因为这个问题是题外话,但却具有历史意义。目前不接受新的答案或互动。 我一直想知道为什么微软为这样一个伟大的平台选择了一个如此奇怪的、对搜索引擎不友好的名称。他们就
.Net Framework .Net .NET Standard的区别 1、.NET Framework 在未来.NET Framework或许成为过去时,目前还是有很多地方在使用的。这一套
如果有选择的话,您会走哪条路? ASP.NET Webforms + ASP.NET AJAX 或 ASP.NET MVC + JavaScript Framework of your Choice
我有一个 Web 服务,它通过专用连接通过 https 使用第三方 Web 服务,我应用了 ServicePointManager.ServerCertificateValidationCallbac
为什么我应该选择ASP.NET Web Application (.NET Framework)而不是ASP.NET Core Web Application (.NET Framework)? 我在
我在网络上没有找到任何关于包含 .NET Standard、.NET Core 和 .NET Framework 项目的 .NET 解决方案的公认命名约定。 就我而言,我们在 .NET 框架项目中有以
.NET Compact 是 .NET 的完美子集吗? 假设我考虑了屏幕大小和其他限制并避免了 .NET Compact 不支持的类和方法,或者 .NET Compact 是一个不同且不兼容的 GUI
我已经阅读了所有我能找到的关于 connectionManagement 中的 maxconnection 设置的文章:即 http://support.microsoft.com/kb/821268
我现在正在使用asp.net mvc,想知道使用内置的Json或 Json.Net哪个是更好的选择,但我不确定一个人是否比另一个人有优势。 另外,如果我确实选择沿用Json.Net的路线,那么我应该选
在 Visual Studio 中,您至少可以创建三种不同类型的类库: 类库(.NET Framework) 类库(.NET 标准) 类库(.NET Core) 虽然第一个是我们多年来一直使用的,但我
.NET 和 ASP.NET 之间有什么区别?它们有什么关系? 最佳答案 ASP.Net 基于 .Net 框架构建,提供有关 Web 开发的附加功能。 你可以去看看wikipedia article
在安装更高版本(3.0)之前,我需要安装.net框架1.1和2.0吗?或者单独安装 3.0 框架就足够了,并为在早期框架版本上编写的软件提供支持?谢谢 ,丽然 最佳答案 不,您不必安装以前的框架。 我
我正在开发一个项目,人们可以“更新”类别,例如更改类别的名称。我收到以下消息 This is called after clicking update 按钮 with the SQL statemen
.NET 类 System.Net.CookieContainer 线程安全吗? --更新:交 key 答复-- 是否有任何方法可以确保异步请求期间修改的变量(即 HttpWebRequest.Coo
我正在使用 JScript.NET 在我编写的 C# WinForms 应用程序中编写脚本。它工作得很好,但我只是尝试在脚本中放置一些异常处理,但我无法弄清楚如何判断我的 C# 代码抛出了哪种类型的异
我需要你的帮助, 比如我有一个小数类型的变量,我想这样取整。 例如 3.0 = 3 3.1 = 4 3.2 = 4 3.3 = 4 3.4 = 4 3.5 = 4 3.6 = 4 3.7 = 4 3.
我使用过这样的代码:http://msdn.microsoft.com/en-us/library/dw70f090.aspx在 ASP.NET 中工作之前访问数据库(2-3 年前)。我没有意识到我正
自 ConfigurationManager .NET Standard 中不存在,检索正在执行的程序集的应用程序设置的最佳方法是什么,无论是 web.config或 appSettings.{env
我是一名优秀的程序员,十分优秀!