VC++程序,加入远程读取数据的问题(HttpOpenRequest?),希望帮教,谢谢!

11138 2003-03-09 10:36:09
有一VC++程序,在用户test登陆程序,工作的之前插入以下部分:

user为用户变量,目前为test,
连接http://test.com/test.txt,读出test.txt的数据,数据格式如下:
a|b|c|d
之后将上面的数据分析出来,得到:
host1=第一部分(就是a)
host2=第一部分(就是b)
host3=第一部分(就是c)
host4=第一部分(就是d)

能不能按照我这个情况,给一个完整的例子让我学习一下啊,因为我确实不太会。。。:)


因为对VC++一点也不了解,前天才接触,希望大家多多指教!!!先谢谢啦~~~

...全文
396 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
11138 2003-04-26
  • 打赏
  • 举报
回复
谢谢大家。。。我结分了哦。。。
用户 昵称 2003-04-07
  • 打赏
  • 举报
回复
代码由Tasehouny(阿甘) 提供,用于读取url的内容。
用户 昵称 2003-04-07
  • 打赏
  • 举报
回复
#include "StdAfx.h"
#include "AmHttpSocket.h"
#include <atlbase.h>
#include <limits.h>

#define AgentName _T("Nimo Software HTTP Retriever 1.0")

//case insensitive search functions...
#ifdef UNICODE
#define _tcsustr wcsustr
#else
#define _tcsustr strustr
#endif
char* strustr(char *source, char *s);
wchar_t* wcsustr(wchar_t *source, wchar_t *s);

char* strustr(char *source, char *s)
{
//make an uppercase copy af source and s
char *csource = strdup(source);
char *cs = strdup(s);
strupr(csource);
strupr(cs);
//find cs in csource...
char *result = strstr(csource, cs);
if (result != NULL)
{
//cs is somewhere in csource
int pos = result - csource;
result = source;
result += pos;
}
//clean up
free(csource);
free(cs);
return result;
}

wchar_t* wcsustr(wchar_t *source, wchar_t *s)
{
//make an uppercase copy af source and s
wchar_t *csource = wcsdup(source);
wchar_t *cs = wcsdup(s);
wcsupr(csource);
wcsupr(cs);
//find cs in csource...
wchar_t *result = wcsstr(csource, cs);
if (result != NULL)
{
//cs is somewhere in csource
int pos = result - csource;
result = source;
result += pos;
}
//clean up
free(csource);
free(cs);
return result;
}

CAmHttpSocket::CAmHttpSocket()
{
LastError = 0;
ReceivedData = NULL;
Headers = NULL;
hIO = NULL;
hIS = NULL;
hCO = NULL;
hIO = InternetOpen(AgentName, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
}

CAmHttpSocket::~CAmHttpSocket()
{
if (ReceivedData != NULL) free(ReceivedData);
if (Headers != NULL) free(Headers);
if (hIO != NULL) InternetCloseHandle(hIO);
if (hIS != NULL) InternetCloseHandle(hIS);
if (hCO != NULL) InternetCloseHandle(hCO);
}

bool CAmHttpSocket::OpenUrl(const TCHAR *url)
{
if (hIS != NULL) InternetCloseHandle(hIS);
hIS = InternetOpenUrl(hIO, url, NULL, 0, HTTP_QUERY_DATE, 0);
if (hIS != NULL) return true;
else
{
LastError = GetLastError();
return false;
}
}

bool CAmHttpSocket::PostUrl(const TCHAR *url, const char *PostData, int PostDataLength)
{
//check length of postdata
if (PostDataLength == -1)
PostDataLength = strlen(PostData);
//some variable that we need...
URL_COMPONENTS uc;
//let's split the url...
uc.dwStructSize = sizeof(uc);
uc.lpszScheme = NULL;
uc.dwSchemeLength = 0;
uc.lpszHostName = NULL;
uc.dwHostNameLength = 1;
uc.nPort = 0;
uc.lpszUserName = NULL;
uc.dwUserNameLength = 0;
uc.lpszPassword = NULL;
uc.dwPasswordLength = 0;
uc.lpszUrlPath = NULL;
uc.dwUrlPathLength = 1;
uc.lpszExtraInfo = NULL;
uc.dwExtraInfoLength = 0;
InternetCrackUrl(url, _tcslen(url), 0, &uc);
//post the data...
if (hCO != NULL) InternetCloseHandle(hCO);
TCHAR *HostName = _tcsdup(uc.lpszHostName);
HostName[uc.dwHostNameLength] = '\0';
TCHAR *FileName = _tcsdup(uc.lpszUrlPath);
FileName[uc.dwUrlPathLength] = '\0';
if (hIS != NULL) InternetCloseHandle(hIS); //if open, close the handle to the connection
DWORD flags;
if (uc.nPort == 80)
{
//we are talking plain http
flags = INTERNET_FLAG_NO_CACHE_WRITE;
}
else
{
//we are talking secure https
flags = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_SECURE |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
}
TCHAR headers[] = _T("Content-Type: application/x-www-form-urlencoded"); //content type for post...
TCHAR szAccept[] = _T("*/*"); //we accept everything...
LPTSTR AcceptTypes[2]={0};
AcceptTypes[0]=szAccept;
hCO = InternetConnect(hIO, HostName, uc.nPort, _T(""), _T(""), INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, 0);
hIS = HttpOpenRequest(hCO, _T("POST"), FileName, NULL, NULL, (LPCTSTR*)AcceptTypes, flags, 0);
if (!HttpSendRequest(hIS, headers, _tcslen(headers), (TCHAR*)PostData, PostDataLength))
{
LastError = GetLastError();
free(HostName);
free(FileName);
return false;
}
free(HostName);
free(FileName);
return true;
}

TCHAR* CAmHttpSocket::GetHeaders(const TCHAR *url)
{
//did we get an url?
if (url == NULL)
{
LastError = -1;
return NULL;
}
//open the url...
OpenUrl(url);
//delete old headers...
if (Headers != NULL) free(Headers);
Headers = (TCHAR*)calloc(1, sizeof(TCHAR));
//get the size headers
DWORD d = 1, d2 = 0;
int i = HttpQueryInfo(hIS, HTTP_QUERY_RAW_HEADERS, Headers, &d, &d2);
//alloc some space for the headers
Headers = (TCHAR*)realloc(Headers, d * sizeof(TCHAR));
if (!HttpQueryInfo(hIS, HTTP_QUERY_RAW_HEADERS, Headers, &d, &d2)) return NULL;
return Headers;
}

char* CAmHttpSocket::GetPage(const TCHAR *url, bool Post, const char *PostData, int PostDataLength)
{
//did we get an url?
if (url == NULL)
{
LastError = -1;
return NULL;
}
//get the page and store it in ReceivedData...
if (Post)
{
//use http post...
if (!PostUrl(url, PostData, PostDataLength)) return NULL;
}
else
{
//use http get
if (!OpenUrl(url)) return NULL;
}
const int rdsize = 8192;
char mr[rdsize];
DWORD rd;
int curpos = 0;
if (ReceivedData != NULL) free(ReceivedData);
ReceivedData = (char*)calloc(rdsize + 1, sizeof(char));
while (InternetReadFile(hIS, mr, rdsize - 1, &rd))
{
if (rd == 0) break;
mr[rd] = '\0';
curpos += rd;
ReceivedData[curpos] = '\0';
strcat(ReceivedData, mr);
ReceivedData = (char*)realloc(ReceivedData, curpos + rdsize);
}
return ReceivedData;
}

TCHAR* CAmHttpSocket::GetHeaderLine(TCHAR *s)
{
//find a line in the headers that contains s, and return a pointer to the line...
if (Headers == NULL) return NULL;
TCHAR *ts = Headers;
if (_tcsustr(ts, s) != NULL) return ts;
while (1)
{
if (*ts == '\0' && ts[1] == '\0') break;
if (*ts == '\0')
{
ts++;
if (_tcsustr(ts, s) != NULL) return ts;
}
else ts++;
}
return NULL;
}

int CAmHttpSocket::GetPageStatusCode()
{
//get the correct header line
TCHAR *s = GetHeaderLine(_T("http"));
if (s == NULL) return 0; //no headers
//find the 3 digit code...
if (_tcslen(s) < 3) return 0; //some error, the string is too short...
while (!(isdigit(s[0]) && isdigit(s[1]) && isdigit(s[2])))
{
if (s[3] == '\0') return 0; //we have reached the end of the string, without finding the number...
s++;
}
//make a copy of s, and return the code
TCHAR *code = _tcsdup(s);
code[3] = '\0'; //remove all text after the 3 digit response code
int result = _ttoi(code);
free(code);
return result;
}
用户 昵称 2003-04-07
  • 打赏
  • 举报
回复
//link with wininet.lib

#pragma once

#include <tchar.h>
#include <windows.h>
#include <wininet.h>

/*
custom errorcodes:
-1: bad url...
*/

class CAmHttpSocket
{
public:
int GetPageStatusCode(); //get the HTTP statuscode for the last received page
TCHAR* GetHeaders(const TCHAR *url); //return a pointer th the headers from an url
CAmHttpSocket();
~CAmHttpSocket();
char* GetPage(const TCHAR *url, bool Post = false, const char *PostData = NULL, int PostDataLength = -1);
//get a page, if post is false, HTTP GET is used othervise HTTP POST is used.
//if PostDataLength is -1 the data must be NULL terminated...
protected:
bool PostUrl(const TCHAR *url, const char *PostData, int PostDataLength = -1);
//open a page using http post
TCHAR* GetHeaderLine(TCHAR *s); //get a specific line from the headers
bool OpenUrl(const TCHAR *url); //open a page using http get
HINTERNET hIO, hIS, hCO;
char *ReceivedData; //the internal databuffer
TCHAR *Headers; //the internal headerbuffer
int LastError; //internal statuscode...
};
11138 2003-03-26
  • 打赏
  • 举报
回复
T
ZhouBoTong 2003-03-09
  • 打赏
  • 举报
回复
1.你用vc写个程序发送一个请求(SendRequest)
2.在Server写一个jsp文件,来接受你上面发送的请求
3.上面的jsp文件访问test.txt文件的内容,并且反馈给Client端的你写的vc程序

上面的一些请求格式,回答格式可以使用XML格式。
11138 2003-03-09
  • 打赏
  • 举报
回复
能不能按照我这个情况,给一个完整的例子让我学习一下啊,因为我确实不太会。。。:)

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧