안녕하세요

프로그램 과정에서 막혔던 문제들에 대한 해결책 정리


페이지 목록

레이블이 libCurl인 게시물을 표시합니다. 모든 게시물 표시
레이블이 libCurl인 게시물을 표시합니다. 모든 게시물 표시

2013년 3월 14일 목요일

[LibCurl] Rest API의 Attachments 필드에 파일 업데이트하기

http://docs.atlassian.com/jira/REST/latest/#id125223

해당 REST API 중

/rest/api/2/issue/{issueIdOrKey}/attachments

위 명령어가 attachments 에 파일을 올리는 URL 입니다.

그리고, 해당 URL을 사용하여 파일으 업데이트 하는 curl 소스는

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt" http://myhost/rest/api/2/issue/TEST-123/attachments

이며, 위 코드의 각 옵션들을 llibCurl로 변경하면 소스 코드는 완성 됩니다.

-u 즉, User Admin 하는 코드는 CURLOPT_USERPWD 옵션을 사용하면 되고,

-X는 Post, Get 등 전송 방식을 선택하는 것이고, Post 방식임으로

CURLOPT_HTTPPOST 방식을 사용하면 됩니다.

-H 는 User가 정한 Header를 넣는 코드로

CURLOPT_HTTPHEADER 를 사용하면 됩니다.

자세한 사용 방법은 각 Option 을 구글링하면 나오게 됩니다.

그리고, -F는 파일을 업로드 하는 것으로 해당 포스팅이 되겠습니다.

한참 해맸던 문제는 코드 에러가 발생하지 않는데, 전송이 되지 않는 것이었습니다.

/* Fill in the file upload field */
 curl_formadd(&formpost,
  &lastptr,
  CURLFORM_COPYNAME, "file",
  CURLFORM_FILE, "abcd.jpg",
  CURLFORM_END);

해당 코드가, 파일 업로드를 하는 필드입니다.

위 방식으로 하면 되는데, 이 때 주의점이

CURLFORM_COPYNAME 필드의 "file" 즉, 이 file 이

curl 명령어의 ""file = @myfile.txt"" 의 file 즉, filename 필드명을 적어 줘야 합니다.

COPYNAME 필드를 file이 아닌 다른 이름으로적으면 filename field를 찾지 못해

파일 전송이 되지 않는 것입니다.

참조 : http://parangbook.tistory.com/193 => filename file와 COPYNAME field 이름 통일을 알게 해준사이트

http://cboard.cprogramming.com/networking-device-communication/76842-file-upload-libcurl.html

=> file 전송에 대한 예시가 있는 사이트

2013년 2월 6일 수요일

[LibCurl] 로그인 예제

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at http://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/
/* Include libraries */
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
size_t func( void* ptr, size_t size, size_t nmemb, void* stream);
int main(void)
{
  CURL *curl;
  CURLcode res;
  char* header;
  char* body;
  header = (char*)calloc(100000, sizeof(char));
  body = (char*)calloc(100000, sizeof(char));
  curl = curl_easy_init(); // Initialization 코드
  if(curl) {
// ID PWD 항목에 해당 ID와 PWD를 넣으면 알아서, ID PWD를 넣어준다.   
// curl_easy_setopt(curl, CURLOPT_USERPWD, " ID : PWD");
// URL 을 사용하는 코드 example.com에 해당 URL 을 넣으면 URL 을 저장한다.
    curl_easy_setopt(curl, CURLOPT_URL, www.example.com);
// FOLLOWLOCATION을 1로 보내면, Redirection 을 모두 따라 간다.
 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); // Allow redirection
// 아래 두 줄은 Post 메세지에 메세지를 실어 보내기 위한 방법이다.
// curl_easy_setopt(curl, CURLOPT_POST, 1);
// curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "ID : PWD");

//WriteFunction 에 Function 등록하면 Callback으로 메세지를 받게 된다.
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, func);
//WriteHeader 옵션으로 헤더 받음
    curl_easy_setopt(curl, CURLOPT_WRITEHEADER, header);
//WriteData 옵션으로 데이터 받음. WriteFunction의 Callback 메세지를 Header와 Body로
//나누어 받은 것
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, body);
// 위에 입력한 옵션들을 실행하는 함수
    res = curl_easy_perform(curl);
    /* always cleanup */
//함수 실행 후 종료 (메모리삭제)
    curl_easy_cleanup(curl);
  }
  printf("\n\nHEADER is\n%s\n\n\n", header);
  printf("\n\nBODY is\n%s\n\n\n", body);
  free(header);
  free(body);
  return 0;
}
size_t func( void* ptr, size_t size, size_t nmemb, void* stream)
{
        strncat( (char*)stream, (char*)ptr, size*nmemb);
        return size*nmemb;
}

2013년 1월 25일 금요일

zlib1 과 libcurl 연동 시 Ordinal 55 문제 해결

참조 : http://fourthslap.blogspot.kr/2011/12/using-libcurl-in-cc-program.html

ordinal 55 coun't not be located in dynamic link library zlib1.dllthen try downloading and adding zlib1.dll file from this link. 
위 링크에 나온 zlib1.dll 파일을 받아서 설치하면 해결 됩니다.

libcurl zlib1.dll 연동 문제 Ordinal 73 에러

Ordinal 73 에러가 나타난다면, zlib1.dll 의 파일이 예전 파일이라서 나타나는 것입니다.

error C2265: '' : reference to a zero-sized array is illegal

출처 : http://hopi.tistory.com/18
http://blog.naver.com/kkn2988?Redirect=Log&logNo=38170248

배열을 0으로 할당하면 다음과 같은... reference to a zero-sized array is illegal 에러 문구가 나오나요?

..  PROGRAM FILES\MICROSOFT PLATFORM SDK\INCLUDE\wspiapi.h  파일의

template <typename __CountofType, size_t _N>
char (&__wspiapi_countof_helper(__CountofType (&_Array)[_N]))[_N];
 이부분을 컴파일 할때

error C2265: '<Unknown>' : reference to a zero-sized array is illegal 이런 에러가 납니다.




답 :
/D "_WSPIAPI_COUNTOF"

C/C++ 옵션에 저 구문을 추가해 주세요 :)
SDK를 설치하면 발생하는 문제랍니다.


위치 : Project Settings -> C/C++ -> Category(General)

Project Opitions에
/D "_WSPIAPI_COUNTOF"
추가 후 재컴파일하면 됩니다.

Unresolved Externals 해결 방법

참조 : http://www.chilkatsoft.com/p/p_124.asp

Win32 Visual C++ 링커 문제를 해결하는 방법을 알려드립니다.

아래와 같이 문제가 있을 때,

ChilkatDbgDll.lib(CryptoSP.obj) : error LNK2001: unresolved external symbol __imp__CryptAcquireContextA@20
ChilkatDbgDll.lib(Hashing.obj) : error LNK2019: unresolved external symbol __imp__CryptGetHashParam@20 referenced in function "public: bool __thiscall Hashing::hashSha1(class DataBuffer const &,class DataBuffer &,class LogBase &)" (?hashSha1@Hashing@@QAE_NABVDataBuffer@@AAV2@AAVLogBase@@@Z)
ChilkatDbgDll.lib(Hashing.obj) : error LNK2019: unresolved external symbol __imp__CryptDestroyHash@4 referenced in function "public: bool __thiscall Hashing::hashSha1(class DataBuffer const &,class DataBuffer &,class LogBase &)" (?hashSha1@Hashing@@QAE_NABVDataBuffer@@AAV2@AAVLogBase@@@Z)

여기에는 몇개의 unresolved 한 함수가 있습니다.
CryptAcquireContextA
CryptGetHashParam
CryptDestroyHash

등인데요. ( "___imp___" 와 "@" 뒤에 나오는 쓰레기 값들은 그냥 C++에서 이름을 알아 보기 힘들게 만들어 놓은 거니깐 무시하세요)
A로 끝나는 함수는 ANSI version의 함수고, W로 끝나는 것은 Unicode 버전 입니다.

A 와 W 를 없애면
CryptAcquireContext
CryptGetHashParam
CryptDestroyHash

함수가 나타나는데, 이제 검색엔진을 이용해서 위 함수를 검색하세요.
search engine (Yahoo, Microsoft, Google, etc.) "site:microsoft.com"
등에서 MSDN result 페이지가 나오면, 맨 밑의 Requirements 세션으로 가서,
무슨 Library가 필요한지 찾아서, VC++ Project에 add 하면 됩니다.

libcurl 설치 중 Libcurl.dll 빌드 시 문제

Linking...
   Creating library DLL-Debug/libcurld_imp.lib and object DLL-Debug/libcurld_imp.exp
md5.obj : error LNK2001: unresolved external symbol __imp__CryptCreateHash@20
md5.obj : error LNK2001: unresolved external symbol __imp__CryptAcquireContextA@20
md5.obj : error LNK2001: unresolved external symbol __imp__CryptHashData@16
md5.obj : error LNK2001: unresolved external symbol __imp__CryptReleaseContext@8
md5.obj : error LNK2001: unresolved external symbol __imp__CryptDestroyHash@4
md5.obj : error LNK2001: unresolved external symbol __imp__CryptGetHashParam@20
DLL-Debug/libcurld.dll : fatal error LNK1120: 6 unresolved externals
Error executing link.exe.

Unresolved Externals 문제는 환경 세팅 (lib 파일을 제공하지 않는 문제) 로 인해 발생하는 거 같습니다.
위 문제에 대한 해결책을 제시합니다.

Project -> Settings -> Link -> Object/library modules 에

Windows Server Platform SDK 의 AdvAPI32.lib 파일을 추가합니다.

아래의 Windows Server Platform SDK를 참조하세요.

http://dreamchallenger.blogspot.kr/2013/01/msvc-60-windows-server-psdk.html

MSVC 6.0 용 Windows Server PSDK 설치 방법

http://social.msdn.microsoft.com/Forums/en/windowssdk/thread/e1147034-9b0b-4494-a5bc-6dfebb6b7eb1

위 사이트가 MSVC 6.0에서 사용할 수 있는 마지막 PSDK

Microsoft Platform SDK Febuary 2003 (Last version with VC6 support)

을 받을 수 있는 사이트 입니다.

다운 받은 후,

cmd 창을 넣어, 다운 받은 폴더로 이동 후

PSDK-FULL Setup Folder 하면 설치파일이 인스톨 됩니다.

예) PSDK-FULL C:\

이렇게 하여, 다운로드 후, setup 파일을 클릭하면 Setup 할 수 있는

인터넷 창이 나타나고, 거기서 Install 하면 됩니다.

Install 완료 후에는,

MSVC 6.0에 환경 설정을 해야 합니다.

MSVC 6.0 -> Tool -> Option -> Directories 로 이동하여,

Include Files 항목에

설치 된 경로의 Include 폴더를 넣은 후, 맨 위로 올립니다.

예) C:\Program Files\MICROSOFT PLATFORM SDK\INCLUDE

library Files 항목에

설치 된 경로의 Lib 폴더를 넣은 후, 맨 위로 올립니다.

이제, Windows Server PSDK 를 사용할 수 있습니다.