概述
首先,服务器是一个JAVA包,用的是REST服务。
//Unicode 模式工程
//转码
std::string UTF8ToString(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
memset(pwBuf, 0, nwLen * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.length(), pwBuf, nwLen);
int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);
WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr = pBuf;
delete []pBuf;
delete []pwBuf;
pBuf = NULL;
pwBuf = NULL;
return retStr;
}
inline BYTE fromHex(const BYTE &x)
{
return isdigit(x) ? x-'0' : x-'A'+10;
}
inline string URLDecode(const string &sIn)
{
string sOut;
for( size_t ix = 0; ix < sIn.size(); ix++ )
{
BYTE ch = 0;
if(sIn[ix]=='%')
{
ch = (fromHex(sIn[ix+1])<<4);
ch |= fromHex(sIn[ix+2]);
ix += 2;
}
else if(sIn[ix] == '+')
{
ch = ' ';
}
else
{
ch = sIn[ix];
}
sOut += (char)ch;
}
return sOut;
}
size_t write_data(char *strdata, size_t size, size_t nmemb, string *strRetData)
{
if (strRetData == NULL)
return 0;
CString csResultData;
CString csReturn;
csResultData.Format(_T("%s"),strdata);
csReturn = csReturn+csResultData;
size_t len = size*nmemb;
strRetData->append(strdata, len);
return len;
}
//下面调用的是libcurl库来请求HTTP;GET方式
int UesrBomInfoGetByNumber(CString projecjName,CString pvsPath,CString csURL,CString& csResult)
{
//bool send_easy_hanler(char* post_url, req_t* req)
//easy handler的句柄
//csURL地址是: http://10.163.98.130:8099/mail/sendhemail?
projecjName=L"A001";pvsPath=L"E:/New folder/A001";
CURL* curl = NULL;
CURLcode res = CURLE_OK;
//HTTP报文头
struct curl_slist* headers = NULL;
//构建HTTP报文头
char c1[256]={0};
projecjName =L"project="+projecjName;
projecjName.Trim();
wcstombs(c1,projecjName,wcslen(projecjName));
const char* tmp_str=c1;
headers = curl_slist_append(headers, tmp_str);
pvsPath=L"&path="+ pvsPath;
pvsPath.Trim();
char c2[256]={0};
wcstombs(c2,pvsPath,wcslen(pvsPath));
const char* tmp_str1=c2;
headers = curl_slist_append(headers, tmp_str1);
/*这个函数只能用一次,如果这个函数在curl_easy_init函数调用时还没调用,
它讲由libcurl库自动调用,所以多线程下最好在主线程中调用一次该函数以防止在线程
中curl_easy_init时多次调用*/
curl_global_init(CURL_GLOBAL_ALL);
//初始化easy handler句柄
curl = curl_easy_init();
if (curl)
{
//设置post请求的url地址
char post_url[256]={0};
wcstombs(post_url,csURL,wcslen(csURL));//具体函数查阅相关文档
// const char* name=c1;
curl_easy_setopt(curl, CURLOPT_URL, post_url);
//curl_easy_setopt(curl, CURLOPT_POST, 1); // post req
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);//用GET方法
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
//设置HTTP头
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
//设置发送超时时间
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
string strdata1;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strdata1);
CString csReturn =L"";
CURLcode RetCode = curl_easy_perform(curl);
CString csText;
//解码
string ret_str = URLDecode(strdata1);
//UTF8_To_string转码
string tmp_string = UTF8ToString(ret_str);
csReturn = (CString)tmp_string.c_str();
//执行单条请求
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
//curl_easy_strerror进行出错打印
cout << "curl_easy_perform() failed:" << curl_easy_strerror(res);
}
curl_slist_free_all(headers);
//这个调用用来结束一个会话.与curl_easy_init配合着用
curl_easy_cleanup(curl);
//在结束libcurl使用的时候,用来对curl_global_init做的工作清理。类似于close的函数
curl_global_cleanup();
}
return 0;
}
最后
以上就是单薄荷花为你收集整理的请求HTTP时返回400,请大神帮忙看看。的全部内容,希望文章能够帮你解决请求HTTP时返回400,请大神帮忙看看。所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复