c#-4.0 - 使用 BouncyCastle 库在 C# 中进行 HTTPS 调用
问题描述
使用 C# 4.0,我需要使用 BouncyCastle 库进行 HTTPS 调用(短故事:Windows XP + TLS 1.2)。
使用以下代码时,我收到“HTTP 错误 400。请求动词无效”。
这是我的代码:
using (var client = new TcpClient("serverName", 443))
{
var sr = new SecureRandom();
var cl = new MyTlsClient();
var protocol = new TlsClientProtocol(client.GetStream(), sr);
protocol.Connect(new MyTlsClient());
using (var stream = protocol.Stream)
{
var hdr = new StringBuilder();
hdr.AppendLine("GET /Url/WebService.asmx?wsdl HTTP/1.1");
hdr.AppendLine("Host: serverName");
hdr.AppendLine("Content-Type: text/xml; charset=utf-8");
hdr.AppendLine("Connection: close");
hdr.AppendLine();
var dataToSend = Encoding.ASCII.GetBytes(hdr.ToString());
sr.NextBytes(dataToSend);
stream.Write(dataToSend, 0, dataToSend.Length);
int totalRead = 0;
string response = "";
byte[] buff = new byte[1000];
do
{
totalRead = stream.Read(buff, 0, buff.Length);
response += Encoding.ASCII.GetString(buff, 0, totalRead);
} while (totalRead == buff.Length);
}
}
class MyTlsClient : DefaultTlsClient
{
public override TlsAuthentication GetAuthentication()
{
return new MyTlsAuthentication();
}
}
class MyTlsAuthentication : TlsAuthentication
{
public TlsCredentials GetClientCredentials(CertificateRequest certificateRequest) { return null; }
public void NotifyServerCertificate(Certificate serverCertificate) { }
}
我已经做了什么:
使用 WireShark 解密 ssl 流并检查请求发送 => 我从未成功解密 ssl 流
使用提琴手解密 https 流 => 提琴手没有检测到,所以我怀疑某些东西可能被严重加密
有任何想法吗 ?
解决方案
感谢 PeterDettman,他给了我解决方案:
我不能使用 sr.NextBytes(instructions),所以代码变成:
using (var client = new TcpClient("serverName", 443))
{
var sr = new SecureRandom();
var cl = new MyTlsClient();
var protocol = new TlsClientProtocol(client.GetStream(), sr);
protocol.Connect(new MyTlsClient());
using (var stream = protocol.Stream)
{
var hdr = new StringBuilder();
hdr.AppendLine("GET /Url/WebService.asmx?wsdl HTTP/1.1");
hdr.AppendLine("Host: serverName");
hdr.AppendLine("Content-Type: text/xml; charset=utf-8");
hdr.AppendLine("Connection: close");
hdr.AppendLine();
var dataToSend = Encoding.ASCII.GetBytes(hdr.ToString());
stream.Write(dataToSend, 0, dataToSend.Length);
int totalRead = 0;
string response = "";
byte[] buff = new byte[1000];
do
{
totalRead = stream.Read(buff, 0, buff.Length);
response += Encoding.ASCII.GetString(buff, 0, totalRead);
} while (totalRead == buff.Length);
}
}
推荐阅读
- c# - 如何分离/过滤输出字符串并将分离的数据写入不同的文本框中?
- php - Wordpress ACF - 如何从类别页面获取字段
- css - 你知道为什么项目不在中心垂直对齐吗?
- graph - 条件图表区域填写 amCharts - amStockCharts
- javascript - Nuxt + Vuex - 如何将 Vuex 模块分解为单独的文件?
- android - 系统广播未传送到广播接收器
- c++ - 使用 flex 和 bison 进行乘法解析
- android - 如何从连续动态添加的所有EditText中获取文本
- react-native - 滚动视图上的文本输入值
- r - R - 读取 Excel 文件并将变量转换为观察值