HTTP包未发送
HTTP packages are not sending
我为MAC OS X Mavericks编写应用程序。它必须是应用程序,它可以向服务器发送POST和GET请求。首先,我尝试发送POST请求。但是,不幸的是,它不起作用,我不知道为什么。当我尝试用嗅探器WireShark捕捉包裹时,它显示没有发送包裹。这是我的代码:
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 faildn" << endl;
return 1;
}*/
//release the message objects
CFRelease(url);
CFRelease(bodyString);
CFRelease(bodyData);
CFRelease(headerFieldName);
CFRelease(headerFieldValue);
CFRelease(bodyDataExt);
bodyDataExt = NULL;
return 0;
}
- 例如,服务器的地址必须采用以下格式:http://10.0.0.25,但不是只有这样的ip->10.0.0.25
我们还必须从响应中检查状态代码,以确保正确的连接。
UInt32 myErrCode = CFHTTPMessageGetResponseStatusCode(myReadStream); if(myErrCode != 200) { cout << "Request faildn" << endl; return 1; }
相关文章:
- 使用jsoncpp-NuGet包时获取未解析的外部符号
- 在确定有实现的情况下获取HTTP 501未实现
- 未找到包'pygtk-2.0'
- Rcpp 包创建符号未找到错误
- 将代码移出类定义时未扩展参数包
- HTTP 标头未在 C++ 中使用 Curl 设置
- xgboost 构建错误:参数包未扩展为"..":
- 定位包时未设置QT的Cmake Dragndrop框架
- 为什么可变参数模板参数包未展开
- cmake/cpack 组件调试信息 rpm 包未出现
- 未使用模板类型定义的同时参数包扩展错误
- AWS-开发工具包-CPP:未解析的符号
- 存储未展开的参数包
- 如何在非 gui 线程上正确创建 QUdpSocket?未发出现读
- 使用RCPP与外部C 库构建R软件包时未定义的参考
- 可变参数模板:错误:参数包未扩展'...'
- C 插座未接收传入的数据包,在Python中工作
- 声明类型包含未展开的参数包'Args'
- libCurl FTP 身份验证数据包未发送
- HTTP包未发送