///////////////////////////////////////////////////////////////
接收程序ASP
<%
num=request("num")
pass=request("pass")
set fs=server.CreateObject("Scripting.FileSystemObject")
set file=fs.OpenTextFile(server.MapPath("http.txt"),8,True)
file.writeline num+"---"+"-"+pass
file.close
set file=nothing
set fs=nothing
response.write "Send OK!"
%>
///////////////////////////////////////////////////////////////
WM上的程序
#include "ConnectManager.h"
CConnectManager m_ConnectManager;
void CGPRSOKDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
HANDLE hConnection;
hConnection = NULL;
////////////////////////////////////////////////////////////
CListBox *pListBox1 = (CListBox*)GetDlgItem(IDC_LIST1);
ASSERT ( pListBox1 );
pListBox1->ResetContent (); //从列表框中删除所有项。
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
CListBox *pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
ASSERT ( pListBox ); //调试用的
CStringArray StrAry;
CString abc,abc2;
m_ConnectManager.EnumNetIdentifier ( StrAry ); //网络描述
for ( int i=0; i {
GPRSGUI=StrAry.GetAt(i);
if( CreateConnection( &hConnection ) == S_OK )
{
abc="连接成功";
}
else
{
abc="连接失败";
}
//abc2=""+StrAry.GetAt(i)+"----"+abc+"";
CString str;
str.Format(L"%s----%s",StrAry.GetAt(i),abc);
//AfxMessageBox ( StrAry.GetAt(i), MB_ICONINFORMATION );
pListBox->AddString (str); //网络描述
}
}
void CGPRSOKDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
CListBox *pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
ASSERT ( pListBox );
DWORD dwIndex = pListBox->GetCurSel(); //获得索引值
if ( dwIndex >= 0 && dwIndex < (DWORD)pListBox->GetCount() )
{
CString test;
pListBox->GetText(dwIndex,test);
SetDlgItemText(IDC_EDIT1,test);
//MessageBox(test);
}
}
void CGPRSOKDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
CString test,test2;
GetDlgItemText(IDC_EDIT1,test);
GetDlgItemText(IDC_EDIT2,test2);
GPRSGUI=test;
/////////////////////////////*MessageBox(GPRSGUI);*/
CString szHeaders;
CString szURL;
//szURL="";
//LPCTSTR p = szURL;
LPCTSTR p = test2;
szHeaders ="EmployeeID: 123456\r\n";
szHeaders+="EmployeePassword: 789";
HANDLE hConnection;
hConnection = NULL;
// HTTPRequest Response;
// memset(&Response, NULL, sizeof(HTTPRequest));
// if (!MCHttp.SendRequestEx(m_VerbType, szURL, HTTP_VERSION_1_1, TEXT("*/*"), TEXT("PervasiveBiz"), szHeaders,
// szSendBuffer, 60000, &Response))
if( CreateConnection( &hConnection ) != S_OK )
MessageBox(_T("连接失败"),_T("错误"));
//return FALSE;
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
if (!AfxParseURL(p, dwServiceType, strServerName, strObject, nPort) ||
dwServiceType != AFX_INET_SERVICE_HTTPS && dwServiceType != AFX_INET_SERVICE_HTTP) //AfxParseURL来为分析映射URL
{
MessageBox(_T("不是HTTP,HTTPS站点"),_T("错误"));
// return -2;
}
//MessageBox(_T("111111"),_T("错误"));
HINTERNET m_hOpen;
HINTERNET m_hConnect;
HINTERNET m_hRequest;
//m_hOpen = InternetOpen(TEXT("PervasiveBiz"), INTERNET_OPEN_TYPE_DIRECT,NULL, 0, 0); //初始化WinINet
m_hOpen = InternetOpenA ("NokiaN70-1/5.0616.2.0.3 Series60/2.8 Profile/MIDP-2.0 Configuration/CLDC-1.1", INTERNET_OPEN_TYPE_PRECONFIG, "10.0.0.172:80", 0, 0 );
if (!m_hOpen)
{
MessageBox(_T("初始化失败!!"),_T("错误"));
// return -1;
}
if (!(m_hConnect = InternetConnect(m_hOpen,strServerName, nPort, NULL, NULL, dwServiceType, 0, 0))) //发送数据
{ //0为真 1为假
InternetCloseHandle(m_hOpen); //关闭连接句柄
// return -1;
}
//MessageBox(_T("2222222"),_T("错误"));
LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL};
DWORD dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
// Open an HTTP request handle.
if (!(m_hRequest = HttpOpenRequest(m_hConnect, TEXT("POST"), strObject, HTTP_VERSION, NULL, (LPCTSTR*)AcceptTypes,dwFlags, 0))) //打开一个 HTTP 请求的句柄
{
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
// return -1;
}
// MessageBox(_T("3333333"),_T("错误"));
//DWORD dwHttpTimeout=TIME_OUT;
DWORD dwHttpTimeout;
if(!InternetSetOption(m_hRequest,INTERNET_OPTION_CONNECT_TIMEOUT,&dwHttpTimeout,sizeof(DWORD))) //设置一个 Internet 选项
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
// return -1;
}
if(!InternetSetOption(m_hRequest,INTERNET_OPTION_SEND_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
// return -1;
}
if(!InternetSetOption(m_hRequest,INTERNET_OPTION_RECEIVE_TIMEOUT,&dwHttpTimeout,sizeof(DWORD)))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);;
// return -1;
}
// MessageBox(NULL, szSendBuffer,szHeaders, 0);
CString szSendBuffer;
// MessageBox(_T("4444444"),_T("错误"));
//////////////////////////////////////////////
//重点要注意的地方,HttpSendRequest的第三个参数必须为多字节编码格式,否则服务器端会出错的。
//花了一天半的时间调试出来的结论。如果不进行转换,服务器端只会接收一半数据,后面的数据将会收不到。
byte* pszData = new byte[szSendBuffer.GetLength()];
char *pConten = new char[szSendBuffer.GetLength()];
memset(pszData,0,szSendBuffer.GetLength());
memset(pConten,0,szSendBuffer.GetLength());
WideCharToMultiByte( CP_ACP, 0, szSendBuffer.GetBuffer(0),
-1, pConten, szSendBuffer.GetLength(), NULL, NULL );
memcpy(pszData, pConten, szSendBuffer.GetLength());
int len = strlen((char*)pszData);
int len2 = szSendBuffer.GetLength();
//MessageBox(NULL, szSendBuffer, str);
if(HttpSendRequest(m_hRequest, szHeaders, szHeaders.GetLength(), pszData, szSendBuffer.GetLength())
== FALSE)
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
// return -1;
}
DWORD dwSize;
HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,NULL);
TCHAR* lpHeaders = new TCHAR [dwSize];
if (!HttpQueryInfo (m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID) lpHeaders, &dwSize, NULL)) //返回HTTP请求关联的信息头
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
// return -1;
}
// MessageBox(_T("555555"),_T("错误"));
/*
//MessageBox(NULL, lpHeaders, _T("HttpQueryInfo"), 0);
TCHAR szHttpStatus[4] = {0};
memcpy(szHttpStatus,lpHeaders+wcslen(_T("HTTP/1.1 ")),3*sizeof(TCHAR));
int dwHttpStatus = 0;
dwHttpStatus =_wtoi(szHttpStatus);
if ((dwHttpStatus<200)||(dwHttpStatus>300))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
// return -1;
}
MessageBox(_T("666666"),_T("错误"));
char cResponse[1024] ;
if(!InternetReadFile (m_hRequest, (LPVOID)(cResponse),32*1024, &dwSize))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
// return -1;
}
CString str;
str.Format(L"%s",cResponse);
MessageBox(str);
MessageBox(_T("77777"),_T("错误"));
if(dwSize == 0)
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
// return -1;
}
HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,NULL);
if (!HttpQueryInfo (m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF,
(LPVOID) lpHeaders, &dwSize, NULL))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
// return -1;
}
memcpy(szHttpStatus,lpHeaders+wcslen(_T("HTTP/1.1 ")),3*sizeof(TCHAR));
dwHttpStatus = _wtoi(szHttpStatus);
if ((dwHttpStatus<200)||(dwHttpStatus>300))
{
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
delete[] lpHeaders;
// return -1;
}
// MessageBox(NULL, lpHeaders, _T("lpHeaders"), 0);
delete[] lpHeaders;
InternetCloseHandle(m_hRequest);
InternetCloseHandle(m_hConnect);
InternetCloseHandle(m_hOpen);
//return SENDDATA_SCUCCEED ;
MessageBox(_T("88888"),_T("错误")); */
}