|
楼主 |
发表于 2014-2-28 16:25:10
|
显示全部楼层
VS10一如既往的天坑,尤其是调用系统API的时候:
//VC发送POST请求的时候始终有异常,暂未处理
[cpp] view plaincopy
/*****************************
* 逸雨清风出品
* CSDN:http://blog.csdn.net/xyydyyqf
* DATE: 2013-6-2
******************************/
#include "stdafx.h"
#include "dwz.h"
#include "afx.h"
#include <afxinet.h>
BOOL WINAPI Main_Proc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
BOOL Main_OnInitDialog(HWND hWnd,HWND hWndFocus,LPARAM lParam);
void Main_OnCommand(HWND hWnd,int id,HWND hwndCtl,UINT codeNotify);
void Main_OnClose(HWND hWnd);
char* Getdwz(CString longwz);
char* Getmydwz(CString longwz,CString myshortwz);
int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAINDIALOG),NULL,Main_Proc);
return 0;
}
BOOL WINAPI Main_Proc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
//开始消息测试
HANDLE_MSG(hWnd,WM_INITDIALOG,Main_OnInitDialog);
HANDLE_MSG(hWnd,WM_COMMAND,Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE,Main_OnClose);
//结束消息测试
}
return FALSE;
}
BOOL Main_OnInitDialog(HWND hWnd,HWND hwndFocus,LPARAM lParam)
{
//设置应用程序图标
HICON hIcon = LoadIcon((HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDI_ICON3));
SendMessage(hWnd,WM_SETICON,TRUE,(LPARAM)hIcon);
SendMessage(hWnd,WM_SETICON,FALSE,(LPARAM)hIcon);
//添加初始化代码
return TRUE;
}
//主窗口关闭
void Main_OnClose(HWND hWnd)
{
EndDialog(hWnd,0);
}
//主窗口命令响应
void Main_OnCommand(HWND hWnd,int id,HWND hwndCtl,UINT codeNotify)
{
switch(id)
{
case IDCREAT:
char *longwz,*myshortwz,*shortwz;
ZeroMemory(longwz,sizeof(longwz));
ZeroMemory(myshortwz,sizeof(myshortwz));
ZeroMemory(shortwz,sizeof(shortwz));
GetDlgItemText(hWnd,IDC_LONG,longwz,sizeof(longwz));
GetDlgItemText(hWnd,IDC_MYSHORT,myshortwz,sizeof(myshortwz));
if (myshortwz == NULL) shortwz = Getdwz(longwz);
else shortwz = Getmydwz(longwz,myshortwz);
SetDlgItemText(hWnd,IDC_SHORT,shortwz);
break;
case IDCLEAR:
SetDlgItemText(hWnd,IDC_SHORT,"");
SetDlgItemText(hWnd,IDC_LONG,"");
SetDlgItemText(hWnd,IDC_MYSHORT,"");
break;
case IDCOPY:
char* source;
GetDlgItemText(hWnd,IDC_SHORT,source,sizeof(source));
if(OpenClipboard(NULL))
{
/*HGLOBLA clipbuffer;
char *buffer;
EmptyClipboard();
clipbuffer=GlobalAlloc(GMEM_DDESHARE,source.GetLength()+1);
buffer=(char *)GlobalLock(clipbuffer);
strcpy(buffer,LPCSTR(source));
GlobalUnLock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();*/
}
break;
default:break;
}
}
char* Getdwz(CString longwz)
{
char chHeader[128];
char chData[128];
char tChars[128];
memset(tChars,0,128);
CInternetSession session;
CHttpConnection* pConnection=NULL;
CHttpFile* pFile=NULL;
INTERNET_PORT nPort=8080;
strcpy(chHeader,"Content-Type: application/x-www-form-urlencoded");
strcpy(chData,"url=");
strcat(chData,longwz);
pConnection = session.GetHttpConnection("http://dwz.cn",nPort);
pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/create.php");
pFile->SendRequest(chHeader,(DWORD)strlen(chHeader),(LPVOID *)chData,strlen(chData));
pFile->Read(tChars,128);
session.Close();
char* shortdwz;
ZeroMemory(shortdwz,sizeof(shortdwz));
for (int i=0;i<sizeof(tChars);i++)
if (tChars == 'h')
{
int k=i,j;
for (j=0;tChars[k]!='"';j++)
{
if (tChars[k] != '\\') shortdwz[j] = tChars[k];
else j--;
k++;
}
shortdwz[j] = '\0';
break;
}
if (shortdwz[7]!='d' || shortdwz[8]!='w' || shortdwz[9]!='z')
{
MessageBox(NULL,"原网址输入错误或无效!","错误",MB_ICONINFORMATION|MB_OK);
return NULL;
}
return shortdwz;
}
char* Getmydwz(CString longwz,CString myshortwz)
{
char chHeader[128];
char chData[128];
char tChars[128];
memset(tChars,0,128);
CInternetSession session;
CHttpConnection* pConnection=NULL;
CHttpFile* pFile=NULL;
INTERNET_PORT nPort=8080;
strcpy(chHeader,"Content-Type: application/x-www-form-urlencoded");
strcpy(chData,"url=");
strcat(chData,longwz);
strcat(chData,"&alias=");
strcat(chData,myshortwz);
pConnection = session.GetHttpConnection("http://dwz.cn",nPort);
pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/create.php");
pFile->SendRequest(chHeader,(DWORD)strlen(chHeader),(LPVOID *)chData,strlen(chData));
pFile->Read(tChars,128);
session.Close();
char* shortdwz;
ZeroMemory(shortdwz,sizeof(shortdwz));
for (int i=0;i<sizeof(tChars);i++)
if (tChars[2] == 's' && tChars[3] == 't' && tChars[4] == 'a' && tChars[5] == 't')
{
MessageBox(NULL,"哦豁,运气撇,被人捷足先登了\n这个短网址已经被定义过了或不合法,","哦豁",MB_ICONINFORMATION|MB_OK;
return NULL;
}
else if (tChars[2] == 'e' && tChars[3] == 'r' && tChars[4] == 'r')
{
MessageBox(NULL,"哦豁,运气撇,被人捷足先登了\n这个长网址已经映射其他短网址了,","哦豁",MB_ICONINFORMATION|MB_OK);
return NULL;
}
else if (tChars == 'h')
{
int k=i,j;
for (j=0;tChars[k]!='"';j++)
{
if (tChars[k] != '\\') shortdwz[j] = tChars[k];
else j--;
k++;
}
shortdwz[j] = '\0';
break;
}
if (shortdwz[7]!='d' || shortdwz[8]!='w' || shortdwz[9]!='z')
{
MessageBox(NULL,"原网址输入错误或无效!","错误",MB_ICONINFORMATION|MB_OK);
return NULL;
}
return shortdwz;
} |
|