/*---------------------------------------------------------------------- | Test5 +---------------------------------------------------------------------*/ static bool Test5(NPT_HttpUrl url) { NPT_LOG_INFO("########### TEST 5 ######################"); NPT_HttpClient client; // first request NPT_HttpRequest request(url, NPT_HTTP_METHOD_POST, NPT_HTTP_PROTOCOL_1_1); NPT_HttpEntity* request_entity = new NPT_HttpEntity(); request_entity->SetInputStream("Testing"); request.SetEntity(request_entity); NPT_HttpResponse* response = NULL; client.SendRequest(request, response); NPT_HttpEntity* entity = NULL; if (response && (entity = response->GetEntity())) { NPT_DataBuffer buffer; if (NPT_FAILED(entity->Load(buffer))) return false; } // try again delete response; response = NULL; request_entity = new NPT_HttpEntity(); request_entity->SetInputStream("Testing2"); request.SetEntity(request_entity); client.SendRequest(request, response); entity = NULL; if (response && (entity = response->GetEntity())) { NPT_DataBuffer buffer; if (NPT_FAILED(entity->Load(buffer))) return false; } return true; }
bool CStreamCtrl::RecvHeaderData(NPT_HttpUrl url,NPT_DataBuffer& buffer) { NPT_HttpClient client; NPT_String rdUrl=url.ToString(); // first request NPT_HttpRequest request(url, NPT_HTTP_METHOD_GET, NPT_HTTP_PROTOCOL_1_1); NPT_HttpResponse* response = NULL; client.SendRequest(request, response); NPT_HttpEntity* entity = NULL; if (response && (entity = response->GetEntity())) { if (NPT_FAILED(entity->Load(buffer))) return false; } else return false; return true; }
bool CStreamCtrl::RecvMediaData(NPT_HttpUrl url,int in_nStartPos, int in_nEndPos,NPT_DataBuffer& buffer) { NPT_HttpClient client; NPT_String rdUrl=url.ToString(); // first request NPT_HttpRequest request(url, NPT_HTTP_METHOD_GET, NPT_HTTP_PROTOCOL_1_1); char range[100]={0}; sprintf(range," bytes=%d-%d",in_nStartPos,in_nEndPos-1); request.GetHeaders().SetHeader(NPT_HTTP_HEADER_RANGE,range); NPT_HttpResponse* response = NULL; client.SendRequest(request, response); NPT_HttpEntity* entity = NULL; if (response && (entity = response->GetEntity())) { if (NPT_FAILED(entity->Load(buffer))) return false; } else return false; return true; }
/*---------------------------------------------------------------------- | ShowResponse +---------------------------------------------------------------------*/ static void ShowResponse(NPT_HttpResponse* response, ShowMode mode) { // show response info NPT_Debug("RESPONSE: protocol=%s, code=%d, reason=%s\n", response->GetProtocol().GetChars(), response->GetStatusCode(), response->GetReasonPhrase().GetChars()); // show headers NPT_HttpHeaders& headers = response->GetHeaders(); NPT_List<NPT_HttpHeader*>::Iterator header = headers.GetHeaders().GetFirstItem(); while (header) { NPT_Debug("%s: %s\n", (const char*)(*header)->GetName(), (const char*)(*header)->GetValue()); ++header; } // show entity NPT_HttpEntity* entity = response->GetEntity(); if (entity != NULL) { NPT_Debug("ENTITY: length=%lld, type=%s, encoding=%s\n", entity->GetContentLength(), entity->GetContentType().GetChars(), entity->GetContentEncoding().GetChars()); switch (mode) { case SHOW_MODE_LOAD: { NPT_DataBuffer body; NPT_Result result =entity->Load(body); if (NPT_FAILED(result)) { NPT_Debug("ERROR: failed to load entity (%d)\n", result); } else { NPT_Debug("BODY: loaded %d bytes\n", (int)body.GetDataSize()); // dump the body NPT_OutputStreamReference output; NPT_File standard_out(NPT_FILE_STANDARD_OUTPUT); standard_out.Open(NPT_FILE_OPEN_MODE_WRITE); standard_out.GetOutputStream(output); NPT_Debug("%s", (char *)body.GetData()); // ÔÝʱ²»Ö§³Ö // output->Write(body.GetData(), body.GetDataSize()); } break; } case SHOW_MODE_STREAM_BLOCKING: { NPT_DataBuffer buffer(4096); NPT_Result result; NPT_InputStreamReference input; entity->GetInputStream(input); do { NPT_Size bytes_read = 0; result = input->Read(buffer.UseData(), 4096, &bytes_read); NPT_Debug("read %d bytes\n", bytes_read); } while (NPT_SUCCEEDED(result)); break; } } } }