首页 > 代码库 > 一个CLI的 的例子

一个CLI的 的例子

1)这是CLI 调用HTTPOST例子

#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::Text;
using namespace System::IO;

// Specify the URL to receive the request.
int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create( args[ 1 ] ));

   // Set some reasonable limits on resources used by this request
   request->MaximumAutomaticRedirections = 4;
   request->MaximumResponseHeadersLength = 4;

   // Set credentials to use for this request.
   request->Credentials = CredentialCache::DefaultCredentials;
   HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
   Console::WriteLine( "Content length is {0}", response->ContentLength );
   Console::WriteLine( "Content type is {0}", response->ContentType );

   // Get the stream associated with the response.
   Stream^ receiveStream = response->GetResponseStream();

   // Pipes the stream to a higher level stream reader with the required encoding format.
   StreamReader^ readStream = gcnew StreamReader( receiveStream,Encoding::UTF8 );
   Console::WriteLine( "Response stream received." );
   Console::WriteLine( readStream->ReadToEnd() );
   response->Close();   readStream->Close();}
2) 这是 CLI string^ 转 char * 例子
 
using namespace System::Runtime::InteropServices;void MethodName(){    String^ nowString = DateTime::Now.ToString("yyyy-MM-dd-HH:mm");    IntPtr ptrToNativeString = Marshal::StringToHGlobalAnsi(nowString);    try    {        CvCapture* capture = cvCreateCameraCapture(0);        IplImage* toSave = cvQueryFrame(capture);        cvSaveImage(static_cast<char*>(ptrToNativeString.ToPointer()), toSave);        cvReleaseImage(&toSave);        cvReleaseCapture(&capture);    }    catch (...)    {        Marshal::FreeHGlobal(ptrToNativeString);        throw;    }    Marshal::FreeHGlobal(ptrToNativeString);}

一个CLI的 的例子