我正在编写一个 http 自动化框架,我的问题是与经过身份验证的 http安全连接进行通信。在做了一些研究之后,我发现了C# 中的SslStream对象,它可以轻松地与我现有的客户端架构集成。问题是; 尽管能够验证我与给定 Web 服务器的连接,但任何“GET [website] HTTP/1.1”命令似乎只返回标题而不是实际网页。
我有一种感觉,我没有正确地形成我的网络请求,但我真的不知道。我整个上午都在做研究,在这个特定问题上找不到很多资源。这是我项目中的代码示例:
private IAsyncResult Request(HttpRequestToken token, ReceiveEvent callback)
{
if (token == null)
throw new Exception("Error. No request information provided. Aborting operation.");
//Setup the TCP Information. (_port is set to 443 for SSL purposes)
var client = new TcpClient(token.Host, _port);
//Get a handle to a networkstream for writing data.
var requestStream = new SslStream(client.GetStream(), false, null);
//Authenticate the request
requestStream.AuthenticateAsClient(token.Host);
//Translate the data.
byte[] sendBuffer = UTF8Encoding.UTF8.GetBytes(token.ToString());
//NOTE: The results of the above command will look like this:
//GET [website] HTTP/1.1
//Host: [host]
//passive: true
//Accepts: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 Fennec/2.0.1
//Send the data.
requestStream.Write(sendBuffer);
//Get the string value with a function that just iterates over the stream.
string val = readStream(requestStream);
//Breakpoint here, this code is not complete but "functions".
return null;
}
本质上,我的代码结果只会返回带有一些标头信息的“HTTP/1.1 200 OK”。除了标头之外,不会返回任何 HTML 或任何其他内容。
根据要求,这里有更多信息:
private string readStream(Stream stream)
{
byte[] resultBuffer = new byte[2048];
string value = "";
//requestStream.BeginRead(resultBuffer, 0, resultBuffer.Length, new AsyncCallback(ReadAsyncCallback), new result() { buffer = resultBuffer, stream = requestStream, handler = callback, asyncResult = null });
do
{
try
{
int read = stream.Read(resultBuffer, 0, resultBuffer.Length);
value += UTF8Encoding.UTF8.GetString(resultBuffer, 0, read);
if (read < resultBuffer.Length)
break;
}
catch { break; }
} while (true);
return value;
}
出于测试目的,我正在尝试访问 google android 开发者门户(因为它使用 SSL)。这只是加载登录页面,除了页面加载请求之外,此时甚至没有传输任何信息。这是我的确切要求。
GET https://accounts.google.com/ServiceLogin HTTP/1.1
Host: accounts.google.com
passive: true
nui: 1
continue: https://market.android.com/publish
followup: https://market.android.com/publish
Accepts: text/html,application/xhtml+xml,application/xml;q=0.9,
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 Fennec/2.0.1
这是服务器响应:
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Strict-Transport-Security: max-age=2592000; includeSubDomains
Set-Cookie: GAPS=1:0bFTJDze2Zz8WL_x3F7-OQfOjEOycg:rG8nLpBEwdG67aU_;Path=/;Expires=Mon, 27-Jan-2014 21:31:48 GMT;Secure;HttpOnly
Set-Cookie: GALX=KaXGmr2TI-I;Path=/;Secure
Cache-control: no-cache, no-store
Pragma: no-cache
Expires: Mon, 01-Jan-1990 00:00:00 GMT
X-Frame-Options: Deny
X-Auto-Login: realm=com.google&args=continue%3Dhttps%253A%252F%252Faccounts.google.com%252FManageAccount
Transfer-Encoding: chunked
Date: Sat, 28 Jan 2012 21:31:48 GMT
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Server: GSE
感谢您花时间看我的问题,我很感激!如果您想从我这里获得更多信息,我很乐意提供。因为我相信我的错误可能是一个愚蠢的格式问题,所以我认为不需要更多信息。
再次,谢谢!