zoukankan      html  css  js  c++  java
  • VC实现表单提交并设置获取COOKIE:

      

       可以尝试通过 HttpAddRequestHeaders 来添加自已的COOKIE:
        char * lpszHeaders = "Cookie: Key=somevalue";
        DWORD dwHeadersLength = strlen(lpszHeaders);
        HttpAddRequestHeaders(hOpenRequest, lpszHeaders, dwHeadersLength,           HTTP_ADDREQ_FLAG_ADD);

         函数会返回成功,但实际上COOKIE可能并没有添加上去,建议在调用HttpOpenRequest时指定 INTERNET_FLAG_NO_COOKIES选项。然而这个选项将阻止WinInet内部添加所需的COOKIE,因此你要负责所有必需的 COOKIE。另一个函数InternetSetCookie也可以设置COOKIE,但我从未用过。

        如果需要检查返回的COOKIE,可以尝试使用HttpQueryInfo,并将其中的dwInfoLevel设置为HTTP_QUERY_SET_COOKIE。

        该方案的可行的,因为cookies实际上就是头的一部分,只要紧贴头的尾部,就没有任何问题。

         下面是MFC代码例子:

    try
    {
         CInternetSession Session ;
         CHttpConnection *pHttpConnect = Session.GetHttpConnection( "www.abc.net" ) ;
         if( pHttpConnect )
         {
            CHttpFile* pFile = pHttpConnect->OpenRequest( CHttpConnection::HTTP_VERB_GET,
                     _T("/memberexists.srf?x="),
                     NULL,
                     1,
                     NULL,
                     NULL,
                     INTERNET_FLAG_NO_COOKIES ); //
        if ( pFile )
        {   
         pFile->AddRequestHeaders("Accept: image/png,*/*;q=0.5");
         pFile->AddRequestHeaders("Accept-Language: zh-cn,zh;q=0.5");
         pFile->AddRequestHeaders("Accept-Encoding: gzip,deflate");
         pFile->AddRequestHeaders("Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7");
         pFile->AddRequestHeaders("Keep-Alive: 300");
         pFile->AddRequestHeaders("Connection: keep-alive");
         pFile->AddRequestHeaders("Cookie: Key=somevalue;domain=abc.com") ;
        
         pFile->SendRequest();
        
         // 返回的HTML
         //CString s ;
         //while (pFile->ReadString(s))
         //str += s ;

         // 取返回的COOKIE
         CString strInfo ;
         DWORD dw = 0 ;
         pFile->QueryInfo(HTTP_QUERY_SET_COOKIE ,strInfo ,&dw) ;

         if (strInfo.IsEmpty()==FALSE)
            AfxMessageBox(strInfo) ;
        

         pFile->Close();
         delete pFile ;
        }
       
       
        pHttpConnect->Close() ;
        delete pHttpConnect ;
       }
    }
    catch( CInternetException *e )
    {
       e->Delete();    
    }

  • 相关阅读:
    Vue 组件封装发布到npm 报错 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
    SpringBoot thymeleaf模板版本,thymeleaf模板更换版本
    SpringBoot application.properties (application.yml)优先级从高到低
    SpringBoot Cmd运行Jar文件指定active文件的命令如下
    Linux各文件夹的作用
    spring + Mybatis + pageHelper + druid 整合源码分享
    MyEclipse weblogic Deploy Location项目名称不正确解决方案
    Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x
    An internal error occurred during: "Launching xxx on WebLogic10.x".
    Spring集成Mybatis,spring4.x整合Mybatis3.x
  • 原文地址:https://www.cnblogs.com/niuniu502/p/2067568.html
Copyright © 2011-2022 走看看