Tôi gặp sự cố tương tự khi cố gắng kết nối với dịch vụ OAuth2 của Google.
tôi đã kết thúc bằng văn bản cho POST bằng tay, không sử dụng WebRequest, như thế này:
TcpClient client = new TcpClient("accounts.google.com", 443);
Stream netStream = client.GetStream();
SslStream sslStream = new SslStream(netStream);
sslStream.AuthenticateAsClient("accounts.google.com");
{
byte[] contentAsBytes = Encoding.ASCII.GetBytes(content.ToString());
StringBuilder msg = new StringBuilder();
msg.AppendLine("POST /o/oauth2/token HTTP/1.1");
msg.AppendLine("Host: accounts.google.com");
msg.AppendLine("Content-Type: application/x-www-form-urlencoded");
msg.AppendLine("Content-Length: " + contentAsBytes.Length.ToString());
msg.AppendLine("");
Debug.WriteLine("Request");
Debug.WriteLine(msg.ToString());
Debug.WriteLine(content.ToString());
byte[] headerAsBytes = Encoding.ASCII.GetBytes(msg.ToString());
sslStream.Write(headerAsBytes);
sslStream.Write(contentAsBytes);
}
Debug.WriteLine("Response");
StreamReader reader = new StreamReader(sslStream);
while (true)
{ // Print the response line by line to the debug stream for inspection.
string line = reader.ReadLine();
if (line == null) break;
Debug.WriteLine(line);
}
Câu trả lời đó được ghi vào dòng phản ứng chứa văn bản lỗi cụ thể mà bạn đang theo đuổi.
Cụ thể, vấn đề của tôi là tôi đã đặt dòng cuối cùng giữa các phần dữ liệu được mã hóa bằng url. Khi tôi lấy chúng ra, mọi thứ đều hiệu quả. Bạn có thể sử dụng một kỹ thuật tương tự để kết nối với dịch vụ của bạn và đọc văn bản lỗi phản hồi thực tế.
Tôi đã trải qua điều tương tự. Tôi đã gửi một đề xuất cho nhóm .NET Framework. Vui lòng bỏ phiếu cho nó: https://connect.microsoft.com/VisualStudio/feedback/details/575075/no-exception-on-httpwebrequest-getresponse#details –