// DMS.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "DMS.h"
#include "tinyxml.h"
#include <time.h>
#include <Windows.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int request(string hostname, string api, string parameters,string &strCookie);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
//declare value;
string strCookie(""),strPersonCode(""),strPassword("");
TiXmlDocument InputDoc("DMS.XML");
BOOL loadFile=InputDoc.LoadFile();
if (loadFile==NULL)
{
cout<<"load file failed!\n";
InputDoc.ErrorDesc();
exit(1);
}
// InputDoc.Print();
TiXmlElement *RootElement=InputDoc.RootElement();
// cout<<RootElement->FirstChild()->Value()<<endl;
TiXmlElement *persons=RootElement->FirstChildElement();
// cout<<persons->FirstChild()->Value()<<endl;
TiXmlAttribute *IDAttribute=persons->FirstAttribute();
/* cout<<IDAttribute->Value()<<endl;*/
TiXmlElement *numbe=persons->FirstChildElement(); //get login job number
/* cout<<numbe->FirstChild()->Value()<<endl;*/
TiXmlElement *password=numbe->NextSiblingElement(); //get login password
/* cout<<password->FirstChild()->Value()<<endl;*/
TiXmlElement *workitemname=password->NextSiblingElement(); //get work item name
/* cout<<"workitemname:"<<workitemname->FirstChild()->Value()<<endl;*/
TiXmlElement *workcontent=workitemname->NextSiblingElement(); //get work content
/* cout<<"content:"<<workcontent->FirstChild()->Value()<<endl;*/
TiXmlElement *starttime=workcontent->NextSiblingElement(); //get work start time
TiXmlElement *endtime=starttime->NextSiblingElement(); //get work start time
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
int Year=sysTime.wYear;
int Mon=sysTime.wMonth;
int Day=sysTime.wDay;
string strTemp;
char temp[100];
string strAddRecord("workitemname=");
strAddRecord.append(workitemname->FirstChild()->Value());
strAddRecord.append("&content=");
strAddRecord.append(workcontent->FirstChild()->Value());
// strAddRecord.append("&ftime=2013-03-01+08%3A30%3A00&ttime=2013-03-01+09%3A00%3A00&hours=0.5");
strAddRecord.append("&ftime=");
char cTimeTemp[100];
memset(cTimeTemp,0,100);
//get start time
int iHours=0,iMinute=0,iSecond=0;
string strTminTemp(starttime->FirstChild()->Value());
iHours=atoi(strTminTemp.substr(0,strTminTemp.find(":")).c_str());
iMinute=atoi(strTminTemp.substr(strTminTemp.find(":")+1,strTminTemp.find("\0")-strTminTemp.find(":")-1).c_str());
sprintf(cTimeTemp,"%04d-%02d-%02d+%02d%%3A%02d%%3A%02d",Year,Mon,Day,iHours,iMinute,iSecond); //get start time
strAddRecord.append(cTimeTemp);
strTminTemp.clear();
//get end time
strTminTemp.append(endtime->FirstChild()->Value());
iHours=atoi(strTminTemp.substr(0,strTminTemp.find(":")).c_str());
iMinute=atoi(strTminTemp.substr(strTminTemp.find(":")+1,strTminTemp.find("\0")-strTminTemp.find(":")-1).c_str());
sprintf(cTimeTemp,"%04d-%02d-%02d+%02d%%3A%02d%%3A%02d",Year,Mon,Day,iHours,iMinute,iSecond); //get start time
strAddRecord.append("&ttime=");
strAddRecord.append(cTimeTemp);
string strAddOtherRecordPaht("/DMS/addOtherRecordHoursAction.do");
string strHost("10.202.10.94");
string strLoginPath("/DMS/loginAction.do");
string strLoginPara("personcode=");
strLoginPara.append(numbe->FirstChild()->Value());
strLoginPara.append("&password=");
strLoginPara.append(password->FirstChild()->Value());
request(strHost,strLoginPath,strLoginPara,strCookie); //get session
request(strHost,strAddOtherRecordPaht,strAddRecord,strCookie); // input record
cout<<"DMS has update ,press any key exit program!\n";
getchar();
return nRetCode;
}
//函数实现
int request(string hostname, string api, string parameters,string &strCookie)
{
CString temp;
// int position;
WSADATA WsaData;
WSAStartup(0x0101, &WsaData);
//初始化socket
struct hostent* host_addr = gethostbyname(hostname.c_str());
if (host_addr == NULL)
{
cout<<"Unable to locate host"<<endl;
return -103;
}
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons((unsigned short)80);
sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
return -100;
}
//建立连接
if (connect(sock, (const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1)
{
cout<<"connect failed"<<endl;
return -101;
}
//初始化发送信息
char send_str[2048] = {0};
//头信息
strcat_s(send_str, "POST ");
strcat_s(send_str, api.c_str());
strcat_s(send_str, " HTTP/1.1\r\n");
strcat_s(send_str, "Host: ");
strcat_s(send_str, hostname.c_str());
strcat_s(send_str, "\r\n");
strcat_s(send_str, "Connection: keep-alive\r\n");
char content_header[100];
sprintf(content_header,"Content-Length: %d\r\n", strlen(parameters.c_str()));
strcat_s(send_str, content_header);
strcat_s(send_str, "Cache-Control: max-age=0\r\n");
strcat_s(send_str, "Origin: http://www.hao123.com\r\n");
strcat_s(send_str, "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/15.0.849.0 Safari/535.1\r\n");
strcat_s(send_str, "Content-Type: application/x-www-form-urlencoded\r\n");
strcat_s(send_str, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
strcat_s(send_str, "Referer: http://www.hao123.com/\r\n");
strcat_s(send_str, "Accept-Encoding: gzip,deflate,sdch\r\n");
strcat_s(send_str, "Accept-Language: zh-CN,zh;q=0.8\r\n");
strcat_s(send_str,"Cookie:");
if (!strCookie.empty())
{
strcat_s(send_str,strCookie.c_str());
}
strcat_s(send_str,"\r\n");
//内容信息
strcat_s(send_str, "\r\n");
strcat_s(send_str, parameters.c_str());
if (send(sock, send_str, strlen(send_str),0) == -1)
{
cout<<"send failed"<<endl;
return -101;
}
// 获取返回信息
char recv_str[4096] = {0};
if (recv(sock, recv_str, sizeof(recv_str), 0) == -1)
{
cout<<"recv failed"<<endl;
return -101;
}
string strHttpResponse;
strHttpResponse.assign(recv_str);
cout<<strHttpResponse.c_str()<<endl;
if (strCookie.empty()) // if strCookie empty then get cook information from server.
{
int iPos=strHttpResponse.find("JSESSIONID");
int iPos1=strHttpResponse.find("path=/");
int iSubLen=iPos1-iPos;
strCookie=strHttpResponse.substr(iPos,iSubLen);
// cout<<strCookie.c_str()<<endl;
// cout<<strHttpResponse[iPos]<<endl;
}
WSACleanup( );
return 0;
}
<?xml version="1.0" ?>
<!-- DMS Login Information -->
<DMS>
<Persons ID="1">
<personcode>xxx</personcode>
<password>xxx</password>
<workitemname>xxx</workitemname>
<content>xxxxx</content>
<starttime>8:00</starttime>
<endtime>17:30</endtime>
</Persons>
</DMS>