- 论坛徽章:
- 0
|
我贴代码吧,大家不要笑话我啊,其实很简单的程序
#include "soapH.h"
#include "stdsoap2.h"
#include "HrNM3000ServiceBinding.nsmap"
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h> /* defines _POSIX_THREADS if pthreads are available */
#if defined(_POSIX_THREADS) || defined(_SC_THREADS)
#include <pthread.h>
#endif
#include <signal.h> /* defines SIGPIPE */
#include <iconv.h>
#define FROM_CHARSET "gb2312"
#define TO_CHARSET "utf-8"
#define MAXCHARSETLEN 1024
const char server[] = "https://192.168.2.125/hrsoft/HrNM3000Service.php";
int convert_code(char *frombuf, char *tobuf, size_t *fromlen, size_t *tolen)
{
iconv_t cd;
cd = iconv_open(TO_CHARSET, FROM_CHARSET);
if(cd < 0){
printf("iconv_open error\n");
return -1;
}
memset(tobuf, 0, *tolen);
if (-1 == iconv(cd, &frombuf, fromlen, &tobuf, tolen)){
printf("iconv err");
return -1;
}
iconv_close(cd);
return 0;
}
int CRYPTO_thread_setup();
void CRYPTO_thread_cleanup();
void sigpipe_handle(int);
int main(int argc, char **argv)
{
struct soap soap;
struct ns2__myPlus value;
// struct ns2__GetScada getvalue;
char *from_value1 = NULL;
char *from_value2 = NULL;
char to_value1[10];
char to_value2[10];
char to_server[1024];
char *soap_action = "usn:HrNM3000#myPlus";
char soap_actionnew[1024];
size_t to_action, from_action;
size_t to_serverlen, from_serverlen;
size_t to_len1, from_len1;
size_t to_len2, from_len2;
char *result = NULL;
/* Init SSL */
if(argc < 3)
{
printf("usage: %s a b\n", argv[0]);
exit(0);
}
from_value1=argv[1];
from_value2=argv[2];
from_len1 = strlen(from_value1);
from_len2 = strlen(from_value2);
from_serverlen = strlen(server);
from_action = strlen(soap_action);
to_len1 = to_len2 =to_serverlen=to_action= MAXCHARSETLEN;
// setlocale(LC_ALL, "");
if(convert_code(from_value1, to_value1, &from_len1, &to_len1) < 0)
printf("convert_code value1 failed\n");
if(convert_code(from_value2, to_value2, &from_len2, &to_len2) < 0)
printf("convert_code value2 failed\n");
if(convert_code(server, to_server, &from_serverlen, &to_serverlen) < 0)
printf("convert_code server failed\n");
if(convert_code(soap_action, soap_actionnew, &from_action, &to_action) < 0)
printf("convert_code action failed\n");
//printf("to_value:%s %s\n", to_value1, to_value2);
soap_ssl_init();
if (CRYPTO_thread_setup())
{ fprintf(stderr, "Cannot setup thread mutex for OpenSSL\n");
exit(1);
}
value.num1 = to_value1;
value.num2 = to_value2;
// getvalue.HostIP = to_value1;
// getvalue.name = to_value2;
/* Init gSOAP context */
soap_init(&soap);
// soap_set_mode(&soap, SOAP_C_UTFSTRING);
//soap.mode |= SOAP_C_UTFSTRING;
if (soap_ssl_client_context(&soap,
/* SOAP_SSL_DEFAULT | SOAP_SSL_SKIP_HOST_CHECK, */ /* if we don't want the host name checks since these will change from machine to machine */
SOAP_SSL_NO_AUTHENTICATION, /* use SOAP_SSL_DEFAULT in production code */
NULL, /* keyfile (cert+key): required only when client must authenticate to server (see SSL docs to create this file) */
NULL, /* password to read the keyfile */
NULL, /* optional cacert file to store trusted certificates, use cacerts.pem for all public certificates issued by common CAs */
NULL, /* optional capath to directory with trusted certificates */
NULL /* if randfile!=NULL: use a file with random data to seed randomness */
))
{
printf("soap_ssl_client_context failed\n");
soap_print_fault(&soap, stderr);
exit(1);
}
soap.connect_timeout = 60; /* try to connect for 1 minute */
soap.send_timeout = soap.recv_timeout = 30; /* if I/O stalls, then timeout after 30 seconds */
if (soap_call_ns2__myPlus(&soap, to_server, soap_actionnew, value.num1, value.num2, &result) == SOAP_OK)
// if (soap_call_ns2__GetScada(&soap, server, "", getvalue.HostIP, getvalue.name, &result) == SOAP_OK)
// if (soap_call_ns2__GetScada(&soap, server, "", "", "", &result) == SOAP_OK)
fprintf(stdout, "Result:%s\n", result);
else
{
printf("error\n");
soap_print_fault(&soap, stderr);
}
soap_destroy(&soap); /* C++ */
soap_end(&soap);
soap_done(&soap);
CRYPTO_thread_cleanup();
return 0;
}
/******************************************************************************\
*
* OpenSSL
*
\******************************************************************************/
#ifdef WITH_OPENSSL
#if defined(WIN32)
# define MUTEX_TYPE HANDLE
# define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
# define MUTEX_CLEANUP(x) CloseHandle(x)
# define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE)
# define MUTEX_UNLOCK(x) ReleaseMutex(x)
# define THREAD_ID GetCurrentThreadId()
#elif defined(_POSIX_THREADS) || defined(_SC_THREADS)
# define MUTEX_TYPE pthread_mutex_t
# define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
# define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
# define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
# define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
# define THREAD_ID pthread_self()
#else
# error "You must define mutex operations appropriate for your platform"
# error "See OpenSSL /threads/th-lock.c on how to implement mutex on your platform"
#endif
struct CRYPTO_dynlock_value
{ MUTEX_TYPE mutex;
};
static MUTEX_TYPE *mutex_buf;
static struct CRYPTO_dynlock_value *dyn_create_function(const char *file, int line)
{ struct CRYPTO_dynlock_value *value;
value = (struct CRYPTO_dynlock_value*)malloc(sizeof(struct CRYPTO_dynlock_value));
if (value)
MUTEX_SETUP(value->mutex);
return value;
}
static void dyn_lock_function(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)
{ if (mode & CRYPTO_LOCK)
MUTEX_LOCK(l->mutex);
else
MUTEX_UNLOCK(l->mutex);
}
static void dyn_destroy_function(struct CRYPTO_dynlock_value *l, const char *file, int line)
{ MUTEX_CLEANUP(l->mutex);
free(l);
}
void locking_function(int mode, int n, const char *file, int line)
{ if (mode & CRYPTO_LOCK)
MUTEX_LOCK(mutex_buf[n]);
else
MUTEX_UNLOCK(mutex_buf[n]);
}
unsigned long id_function()
{ return (unsigned long)THREAD_ID;
}
int CRYPTO_thread_setup()
{ int i;
mutex_buf = (MUTEX_TYPE*)malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
if (!mutex_buf)
return SOAP_EOM;
for (i = 0; i < CRYPTO_num_locks(); i++)
MUTEX_SETUP(mutex_buf[i]);
CRYPTO_set_id_callback(id_function);
CRYPTO_set_locking_callback(locking_function);
CRYPTO_set_dynlock_create_callback(dyn_create_function);
CRYPTO_set_dynlock_lock_callback(dyn_lock_function);
CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function);
return SOAP_OK;
}
void CRYPTO_thread_cleanup()
{ int i;
if (!mutex_buf)
return;
CRYPTO_set_id_callback(NULL);
CRYPTO_set_locking_callback(NULL);
CRYPTO_set_dynlock_create_callback(NULL);
CRYPTO_set_dynlock_lock_callback(NULL);
CRYPTO_set_dynlock_destroy_callback(NULL);
for (i = 0; i < CRYPTO_num_locks(); i++)
MUTEX_CLEANUP(mutex_buf[i]);
free(mutex_buf);
mutex_buf = NULL;
}
#else
/* OpenSSL not used, e.g. GNUTLS is used */
int CRYPTO_thread_setup()
{ return SOAP_OK;
}
void CRYPTO_thread_cleanup()
{ }
#endif
/******************************************************************************\
*
* SIGPIPE
*
\******************************************************************************/
void sigpipe_handle(int x) { } |
|