Col*_*ill 3 c# ssl https webclient sslstream
我正在编写一个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;
}
Run Code Online (Sandbox Code Playgroud)
从本质上讲,我的代码的结果只会返回带有一些标题信息的"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;
}
Run Code Online (Sandbox Code Playgroud)
出于测试目的,我试图访问谷歌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
Run Code Online (Sandbox Code Playgroud)
这是服务器响应:
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
Run Code Online (Sandbox Code Playgroud)
感谢您抽出宝贵时间来查看我的问题,我很感激!如果您想了解我的更多信息,我很乐意给予.因为我认为我的错误可能是一个愚蠢的格式问题,所以我认为不需要更多的信息.
再次,谢谢!
该readStream函数在读取少于完整缓冲区时终止.
但是,这只是意味着现在没有更多数据可用; 未来可能会有更多可用的东西.
在stream.Read返回零字节之前不要停止读取.
| 归档时间: |
|
| 查看次数: |
3946 次 |
| 最近记录: |