代码改变世界

测试一下

2010-01-29 12:54  刘少侠  阅读(146)  评论(0编辑  收藏  举报
public string getSessionKey( string auth_code )
{
try
{
// Create a request for the URL.
WebRequest request = WebRequest.Create( "http://container.api.tbsandbox.com/container?authcode=" + auth_code );
// Get the response.
HttpWebResponse response = ( HttpWebResponse )request.GetResponse( );
if ( response.StatusCode != HttpStatusCode.OK ) return "Error";
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream( );
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader( dataStream );
// Read the content.
string responseFromServer = reader.ReadToEnd( );
string[ ] sArray = responseFromServer.Split( '&' );
foreach ( string i in sArray )
{
if ( i.ToString( ).StartsWith( "top_session" ) ) return i.ToString( ).Substring( i.ToString( ).IndexOf( "=" ) + 1 );
}
// Display the content.
// Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close( );
dataStream.Close( );
response.Close( );
return responseFromServer;
}
catch ( WebException e )
{
return "Error:" + e.Status;
}
catch ( Exception e )
{
return "Error:" + e.Message;
}
}