2014-09-22 9 views
0

저는 Mac OS X Mavericks 용 응용 프로그램을 작성합니다. POST 및 GET 요청을 서버에 보낼 수있는 응용 프로그램이어야합니다. 우선, POST 요청을 보내려고합니다. 그러나 불행히도, 그것은 작동하지 않으며 나는 왜 모른다. 스니퍼 WireShark로 패키지를 잡으려고 할 때 패키지가 발송되지 않았 음을 보여줍니다. 이것은 내 코드입니다.HTTP 패키지가 전송되지 않습니다.

UInt32 Login(wchar_t* wstrIpAddress) 
{ 
    size_t size = wstrIpAddress.length(); 

    //create request body 
    CFStringRef bodyString = CFSTR(""); 
    CFDataRef bodyData = CFStringCreateExternalRepresentation(kCFAllocatorDefault, 
                 bodyString, 
                 kCFStringEncodingUTF8, 
                 0); 
    if(bodyData == NULL) 
    { 
     cout << "CFStringCreateExternalRepresentation() could not convert the characters to the specified encoding. " 
     "Error code: " 
     << errno << endl; 
     return 2; 
    } 

    //create headers 
    CFStringRef headerFieldName = CFSTR("Content-Type"); 
    CFStringRef headerFieldValue = CFSTR("application/x-www-form-urlencoded;charset=base64"); 

    //fix coding wstrIpAddress from wstring to UInt8 
    CFStringEncoding encoding = 
    (CFByteOrderLittleEndian == CFByteOrderGetCurrent()) ? kCFStringEncodingUTF32LE : kCFStringEncodingUTF32BE; 

    CFIndex wstrIpAddressLen = (wcslen(wstrIpAddress.c_str())) * sizeof(wchar_t); 

    CFStringRef myUrl = CFStringCreateWithBytes(kCFAllocatorDefault, 
             (UInt8*)wstrIpAddress.c_str(), 
             wstrIpAddressLen, 
             encoding, 
             false); 
    if(myUrl == NULL) 
    { 
     cout << "CFStringCreateWithBytes() had a problem with creating the object. Error code: " << errno << endl; 
     return 3; 
    } 

    //CFStringRef urlEscaped = CFURLCreateStringByAddingPercentEscapes(NULL, myUrl, NULL, NULL, kCFStringEncodingUTF8); 

    CFURLRef url = CFURLCreateWithString(kCFAllocatorDefault, myUrl, 0); 
    if(url == NULL) 
    { 
     cout << "CFURLCreateWithString() had a problem with creating the object. Error code: " << errno << endl; 
     return 4; 
    } 

    CFStringRef requestMethod = CFSTR("POST"); 
    CFHTTPMessageRef myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, url, kCFHTTPVersion1_1); 
    if(myRequest == NULL) 
    { 
     cout << "CFHTTPMessageCreateRequest() had a problem with creating the object. Error code: " << errno << endl; 
     return 5; 
    } 

    CFDataRef bodyDataExt = CFStringCreateExternalRepresentation(kCFAllocatorDefault, 
                 bodyString, 
                 kCFStringEncodingUTF8, 
                 0); 
    if(bodyDataExt == NULL) 
    { 
     cout << "CFStringCreateExternalRepresentation() could not convert the characters to the specified encoding. " 
     "Error code: " 
     << errno << endl; 
     return 6; 
    } 

    CFHTTPMessageSetBody(myRequest, bodyDataExt); 
    CFHTTPMessageSetHeaderFieldValue(myRequest, headerFieldName, headerFieldValue); 
    CFReadStreamRef myReadStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, myRequest); 
    //CFErrorRef copyError = CFReadStreamCopyError(myReadStream); 
    //cout << copyError << endl; 
    CFRelease(myRequest); 
    myRequest = NULL; 

    //you could release the CFHTTP request object immediately after calling CFReadStreamCreateForHTTPRequest 
    //Because the read stream opens a socket connection with the server specified by the myUrl parameter when the CFHTTP request was created, some 
    //amount of time must be allowed to pass before the stream is considered to be open 

    //Opening the read stream also causes the request to be serialized and sent 
    Boolean bRes = CFReadStreamOpen(myReadStream); 
    if(bRes == FALSE) 
    { 
     CFStreamError myErr = CFReadStreamGetError(myReadStream); 
     // An error has occurred. 
     if (myErr.domain == kCFStreamErrorDomainPOSIX) { 
     // Interpret myErr.error as a UNIX errno. 
     } 
     else if (myErr.domain == kCFStreamErrorDomainMacOSStatus) { 
     // Interpret myErr.error as a MacOS error code. 
     OSStatus macError = (OSStatus)myErr.error; 
     // Check other error domains. 
     } 
     cout << "ReadStreamOpen error with error code: " << errno << endl;; 
     return 7; 
    } 
    //UInt32 myErrCode = CFHTTPMessageGetResponseStatusCode(myReadStream); 
    /*if(myErrCode != 200) 
    { 
     cout << "Request faild\n" << endl; 
     return 1; 
    }*/ 

    //release the message objects 
    CFRelease(url); 
    CFRelease(bodyString); 
    CFRelease(bodyData); 
    CFRelease(headerFieldName); 
    CFRelease(headerFieldValue); 
    CFRelease(bodyDataExt); 
    bodyDataExt = NULL; 
    return 0; 
} 
+0

당신은 이미 디버거를 통해 단계나요? –

+0

@ πάνταῥεῖ, 물론 그렇습니다. 그러나 그것은 도움이되지 않았습니다. ((( – neo

+0

나는 대답을 찾지만 내 평판 때문에 나를 게시 할 수 없습니다. ((내가 무엇을 할 수 있습니까? ? – neo

답변

0

답변을 찾았습니다.

  1. 서버에 주소, 예를 들어, 같은 형식이어야합니다 : http://10.0.0.25하지만 만 같이하지 않는 IP -> 10.0.0.25.
  2. 올바른 응답을 보장하는 응답의 상태 코드도 확인해야합니다.

    UInt32 myErrCode = CFHTTPMessageGetResponseStatusCode(myReadStream); 
    if(myErrCode != 200) 
    { 
        cout << "Request faild\n" << endl; 
        return 1; 
    }