symbian的HTTP引擎中对302、301事件的处理

当发起一个Get请求至服务器时,有时候会返回一个页面跳转的信息,这个信息体现在HTTPHEARDER中的具体数值就是302或者301,两者对于用户来说并没有什么本质区别。

以下是处理方法:

1.可以在MHFRunL中直接处理:

void CHttpEngine::MHFRunL(RHTTPTransaction aTransaction,
		const THTTPEvent& aEvent)
{
  switch (aEvent.iStatus)
  {
  case THTTPEvent::ERedirectedTemporarily:
  case THTTPEvent::ERedirectedPermanently:
  {
    iUri = aTransaction.Request().URI().UriDes().Alloc();
  }
}

其中ERedirectedTemporarily和ERedirectedPermanently分别对应301和302,系统会自动重新发送一个get请求至跳转后的url。

但是我在用wifi和cmwap的接入点进行测试的时候发现迟迟得不到重新发送的get请求返回的数据。于是我只能自己手动重新发送:

void CHttpEngine::MHFRunL(RHTTPTransaction aTransaction,
		const THTTPEvent& aEvent)
{
  switch (aEvent.iStatus)
  {
  case THTTPEvent::ERedirectedTemporarily:
  case THTTPEvent::ERedirectedPermanently:
  {
    iUri = aTransaction.Request().URI().UriDes().Alloc();
    aTransaction.Close();//关闭当前会话
    SendGetL(*iUri);//发送一个新的get请求
  }
}

2.另外一种处理方式是将http引擎中处理Redirect的过滤器移除:

	RStringPool pool = iSession.StringPool();
	iSession.FilterCollection().RemoveFilter( pool.StringF( HTTP::ERedirect, RHTTPSession::GetTable() ) );

然后在MHFRunL中进行判断:

void CHttpEngine::MHFRunL(RHTTPTransaction aTransaction,
		const THTTPEvent& aEvent)
{
	switch (aEvent.iStatus)
	{
	case THTTPEvent::EGotResponseHeaders: 
	{
		RHTTPResponse resp = aTransaction.Response();
      iResponseCode = resp.StatusCode();
		if(iResponseCode == 302 || iResponseCode == 301)
		{
			RStringF sLocation = iSession.StringPool().StringF(HTTP::ELocation, RHTTPSession::GetTable());
			THTTPHdrVal headerLocation;
			resp.GetHeaderCollection().GetField(sLocation, 0, headerLocation);
			iLocationUrl = headerLocation.StrF().DesC().Alloc();//获取跳转的url地址
         sLocation.Close();
         aTransaction.Close();
         SendGetL(*iLocationUrl);
		}
  }
  }
}

用这种方式在测试时同样出现了在wifi和cmwap接入点中重新get后无响应的情况。

本文为脚盆同学原创,如需转载请注明出处。

原文地址:https://www.cnblogs.com/ziip/p/1793761.html