求解c++爬虫,这个代码为何老是(hash_set)错误?

老是编译错误好难呀,越改越错只好让大佬们帮忙看看了!用的是vs2019

报错

    E0035   #error 指令:  <hash_set> is deprecated and will be REMOVED. Please use <unordered_set>. You can define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS to acknowledge that you have received this warning.  c爬虫 C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.24.28314\include\hash_set 21  
错误  C1189   #error:  <hash_set> is deprecated and will be REMOVED. Please use <unordered_set>. You can define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS to acknowledge that you have received this warning. c爬虫 C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.24.28314\include\hash_set 22  

设计爬虫程序并将爬取到内容存放在文件中。
要求:
1. 可将指定URL及其级联的三级网页下面的所有URL及相应内容找出来。
2. 以BFS或者DFS策略搜索级联页面,设计相应的数据结构与文件,将URL与页面内容(文本形式)保存。
3. 提供简单的搜索引擎功能(给定字符串,如果在级联的三级网页下面下存在,则返回相应的URL)



#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include "winsock2.h"
#include <time.h>
#include <queue>
#include <hash_set>
#pragma comment(lib, "ws2_32.lib") 
using namespace std;
#define DEFAULT_PAGE_BUF_SIZE 1048576

queue<string> hrefUrl;
hash_set<string> visitedUrl;
hash_set<string> visitedImg;
int g_ImgCnt = 1;//图片编号

bool ParseURL(const string& url, string& host, string& resource) {//解析URL,解析出主机名,资源名
    if (strlen(url.c_str()) > 2000) {
        return false;
    }
    const char* pos = strstr(url.c_str(), "http://");
    if (pos == NULL) pos = url.c_str();
    else pos += strlen("http://");
    char pHost[100];
    char pResource[2000];
    if (strstr(pos, "/") == 0)
    {
        pResource[0] = '/'; pResource[1] = 0;
        strcpy(pHost, pos);
    }
    else
        sscanf(pos, "%[^/]%s", pHost, pResource);

    host = pHost;
    resource = pResource;
    return true;
}
//使用Get请求,得到响应
bool GetHttpResponse(const string& url, char*& response, int& bytesRead) {
    string host, resource;
    if (!ParseURL(url, host, resource)) {
        cout << "获取get请求失败" << endl;
        return false;
    }

    //建立socket
    struct hostent* hp = gethostbyname(host.c_str());
    if (hp == NULL) {
        cout << " host 地址错误" << endl;
        return false;
    }

    SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == -1 || sock == -2) {
        cout << " sock创建失败" << endl;
        return false;
    }
    //建立服务器地址
    SOCKADDR_IN sa;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(80);
    memcpy(&sa.sin_addr, hp->h_addr, 4);
    //建立连接
    if (0 != connect(sock, (SOCKADDR*)&sa, sizeof(sa))) {
        cout << "不能连接: " << url << endl;
        closesocket(sock);
        return false;
    };
    //准备发送数据
    string request = "GET " + resource + " HTTP/1.1\r\nHost:" + host + "\r\nConnection:Close\r\n\r\n";//字符串拼接
    //发送数据
    if (SOCKET_ERROR == send(sock, request.c_str(), request.size(), 0)) {//request.size()=strlen(request.c_str)
        cout << "send error" << endl;
        closesocket(sock);
        return false;
    }
    //接收数据
    int m_nContentLength = DEFAULT_PAGE_BUF_SIZE;
    char* pageBuf = (char*)malloc(m_nContentLength);
    memset(pageBuf, 0, m_nContentLength);
    bytesRead = 0;
    int ret = 1;
    while (ret > 0)
    {
        ret = recv(sock, pageBuf + bytesRead, m_nContentLength - bytesRead, 0);
        if (ret > 0){
            bytesRead += ret;
        }
        if (m_nContentLength - bytesRead < 100){
            m_nContentLength *= 2;
            pageBuf = (char*)realloc(pageBuf, m_nContentLength);
        }
        cout << ret << " ";
    }
    cout << endl;
    pageBuf[bytesRead] = '\0';
    response = pageBuf;
    closesocket(sock);
    return true;
}
//提取所有的URL以及图片URL
void HTMLParse(string& htmlResponse, vector<string>& imgurls, const string& host) {
    const char* p = htmlResponse.c_str();
    const char* tag = "href=\"";//href的内容 就是指要跳转的路由 或 方法 
    const char* pos = strstr(p, tag);
    ofstream ofile("url.txt", ios::app);//以追加的方式打开文件
    while (pos){
        pos += strlen(tag);
        const char* nextQ = strstr(pos, "\"");
        if (nextQ){
            char* url = new char[nextQ - pos + 1];
            sscanf(pos, "%[^\"]", url);
            string surl = url;  // 转换成string类型,可以自动释放内存
            if (visitedUrl.find(surl) == visitedUrl.end()) {//   visitedUrl.find( surl )=visitedUrl.end() 表示visitedUrl中没有sur1
                visitedUrl.insert(surl);
                ofile << surl << endl;
                hrefUrl.push(surl);//将sur1插到队列末
            }
            pos = strstr(pos, tag);
            delete[] url; 
        }
    }
    ofile << endl << endl;
    ofile.close();
    tag = "<img ";
    const char* att1 = "src=\"";//规定图像的 URL。
    const char* att2 = "lazy_src=\"";
    const char* pos0 = strstr(p, tag);
    while (pos0){
        pos0 += strlen(tag);
        const char* pos2 = strstr(pos0, att2);
        if (!pos2 || pos2 > strstr(pos0, ">")){
            pos = strstr(pos0, att1);//非延迟加载项
            if (!pos)
            {
                pos0 = strstr(att1, tag);
                continue;
            }
            else
            {
                pos = pos + strlen(att1);
            }
        }else {
            pos = pos2 + strlen(att2);
        }

        const char* nextQ = strstr(pos, "\"");
        if (nextQ){
            char* url = new char[nextQ - pos + 1];
            sscanf(pos, "%[^\"]", url);
            cout << url << endl;
            string imgUrl = url;
            if (visitedImg.find(imgUrl) == visitedImg.end())
            {
                visitedImg.insert(imgUrl);
                imgurls.push_back(imgUrl);//imgurls作为模板队列,push_back的参数由单个字符变成string类型
            }
            pos0 = strstr(pos0, tag);
            delete[] url;
        }
    }
    cout << "网页请求成功!" << endl;
}

//把URL转化为文件名
string ToFileName(const string& url) {
    string fileName;
    fileName.resize(url.size());
    int k = 0;
    for (int i = 0; i < (int)url.size(); i++) {
        char ch = url[i];
        if (ch != '\\' && ch != '/' && ch != ':' && ch != '*' && ch != '?' && ch != '"' && ch != '<' && ch != '>' && ch != '|')
            fileName[k++] = ch;
    }
    return fileName.substr(0, k) + ".txt";//从第0位开始,长度为k的字符串 + .txt
}

//下载图片到img文件夹
void DownLoadImg(vector<string>& imgurls, const string& url) {

    //生成保存该url下图片的文件夹
    string foldname = ToFileName(url);
    foldname = "./img/" + foldname;
    if (!CreateDirectoryA(foldname.c_str(), NULL))
        cout << "不能创建文件:" << foldname << endl;
    char* image;
    int byteRead;
    for (int i = 0; i < imgurls.size(); i++)
    {
        //判断是否为图片
        string str = imgurls[i];
        int pos = str.find_last_of(".");
        if (pos == string::npos)
            continue;
        else
        {
            string ext = str.substr(pos + 1, str.size() - pos - 1);
            if (ext != "bmp" && ext != "jpg" && ext != "jpeg" && ext != "gif" && ext != "png")
                continue;
        }
        //下载其中的内容
        if (GetHttpResponse(imgurls[i], image, byteRead))
        {
            if (strlen(image) == 0)
            {
                continue;
            }
            const char* p = image;
            const char* pos = strstr(p, "\r\n\r\n") + strlen("\r\n\r\n");
            int index = imgurls[i].find_last_of("/");
            if (index != string::npos)
            {
                string imgname = imgurls[i].substr(index, imgurls[i].size());
                ofstream ofile(foldname + imgname, ios::binary);
                if (!ofile.is_open())
                    continue;
                cout << g_ImgCnt++ << foldname + imgname << endl;
                ofile.write(pos, byteRead - (pos - p));
                ofile.close();
            }
            free(image);
        }
    }
}

//广度遍历
void BFS(const string& url) {
    char* response;
    int bytes;
    // 获取网页的相应,放入response中。
    if (!GetHttpResponse(url, response, bytes)) {
        cout << "网页获取错误!." << endl;
        return;
    }
    string httpResponse = response;
    free(response);
    string filename = ToFileName(url);
    ofstream ofile("./html/" + filename);
    if (ofile.is_open()) {
        ofile << httpResponse << endl; // 保存该网页的文本内容
        ofile.close();
    }
    vector<string> imgurls;
    HTMLParse(httpResponse, imgurls, url);//解析该网页的所有图片链接,放入imgurls里面
    DownLoadImg(imgurls, url);
}
//文件内容搜索 
void Fstring(string& urlar) {
    int i = 0;
    cout << "请输入需要查找的字符串";
    string defo, urltxt;
    cin >> defo;
    ifstream ifile;
    do {
        if (urlar[i] == NULL) {
            cout << "没有找到所要内容";
            ifile.close();
            return;
        }
        ifstream ifile("./html/" + urlar[i++]);
        ifile >> urltxt;
    } while (urltxt.find(defo) == string::npos);//字符串对比 
    cout << "搜索内容位于:" << urlar[i - 1];
    ifile.close();
}

int main(){
    //初始化socket,用于tcp网络连接
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        return 0;
    }
    // 创建文件夹,保存图片和网页文本文件
    CreateDirectoryA("./img", 0);
    CreateDirectoryA("./html", 0);
    string urlStart = "http://hao.360.cn/meinvdaohang.html";
    //cout << "请输入url";
    //cin >> urlStart;
    BFS(urlStart);
    string urlar[2000];
    int i = 0;
    // 访问过的网址保存起来
    visitedUrl.insert(urlStart);
    while (hrefUrl.size() != 0) {
        string url = hrefUrl.front();  // 从队列的最开始取出一个网址
        cout << url << endl;
        BFS(url);                      // 遍历提取出来的那个网页,找它里面的超链接网页放入hrefUrl,下载它里面的文本,图片
        urlar[i++] = ToFileName(url);
        hrefUrl.pop();                 // 遍历完之后,删除这个网址
    }
    int p;
    cout << "是否需要查找内容?(1,查找,2退出):";
    cin >> p;
    if (p == 1)
        Fstring(*urlar);
    WSACleanup();//解除socket

    return 0;
}
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include "winsock2.h"
#include <time.h>
#include <queue>
#include <hash_set>
#pragma comment(lib, "ws2_32.lib") 
using namespace std;
#define DEFAULT_PAGE_BUF_SIZE 1048576

queue<string> hrefUrl;
hash_set<string> visitedUrl;
hash_set<string> visitedImg;
int g_ImgCnt = 1;//图片编号

bool ParseURL(const string& url, string& host, string& resource) {//解析URL,解析出主机名,资源名
    if (strlen(url.c_str()) > 2000) {
        return false;
    }
    const char* pos = strstr(url.c_str(), "http://");
    if (pos == NULL) pos = url.c_str();
    else pos += strlen("http://");
    char pHost[100];
    char pResource[2000];
    if (strstr(pos, "/") == 0)
    {
        pResource[0] = '/'; pResource[1] = 0;
        strcpy(pHost, pos);
    }
    else
        sscanf(pos, "%[^/]%s", pHost, pResource);

    host = pHost;
    resource = pResource;
    return true;
}
//使用Get请求,得到响应
bool GetHttpResponse(const string& url, char*& response, int& bytesRead) {
    string host, resource;
    if (!ParseURL(url, host, resource)) {
        cout << "获取get请求失败" << endl;
        return false;
    }

    //建立socket
    struct hostent* hp = gethostbyname(host.c_str());
    if (hp == NULL) {
        cout << " host 地址错误" << endl;
        return false;
    }

    SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == -1 || sock == -2) {
        cout << " sock创建失败" << endl;
        return false;
    }
    //建立服务器地址
    SOCKADDR_IN sa;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(80);
    memcpy(&sa.sin_addr, hp->h_addr, 4);
    //建立连接
    if (0 != connect(sock, (SOCKADDR*)&sa, sizeof(sa))) {
        cout << "不能连接: " << url << endl;
        closesocket(sock);
        return false;
    };
    //准备发送数据
    string request = "GET " + resource + " HTTP/1.1\r\nHost:" + host + "\r\nConnection:Close\r\n\r\n";//字符串拼接
    //发送数据
    if (SOCKET_ERROR == send(sock, request.c_str(), request.size(), 0)) {//request.size()=strlen(request.c_str)
        cout << "send error" << endl;
        closesocket(sock);
        return false;
    }
    //接收数据
    int m_nContentLength = DEFAULT_PAGE_BUF_SIZE;
    char* pageBuf = (char*)malloc(m_nContentLength);
    memset(pageBuf, 0, m_nContentLength);
    bytesRead = 0;
    int ret = 1;
    while (ret > 0)
    {
        ret = recv(sock, pageBuf + bytesRead, m_nContentLength - bytesRead, 0);
        if (ret > 0){
            bytesRead += ret;
        }
        if (m_nContentLength - bytesRead < 100){
            m_nContentLength *= 2;
            pageBuf = (char*)realloc(pageBuf, m_nContentLength);
        }
        cout << ret << " ";
    }
    cout << endl;
    pageBuf[bytesRead] = '\0';
    response = pageBuf;
    closesocket(sock);
    return true;
}
//提取所有的URL以及图片URL
void HTMLParse(string& htmlResponse, vector<string>& imgurls, const string& host) {
    const char* p = htmlResponse.c_str();
    const char* tag = "href=\"";//href的内容 就是指要跳转的路由 或 方法 
    const char* pos = strstr(p, tag);
    ofstream ofile("url.txt", ios::app);//以追加的方式打开文件
    while (pos){
        pos += strlen(tag);
        const char* nextQ = strstr(pos, "\"");
        if (nextQ){
            char* url = new char[nextQ - pos + 1];
            sscanf(pos, "%[^\"]", url);
            string surl = url;  // 转换成string类型,可以自动释放内存
            if (visitedUrl.find(surl) == visitedUrl.end()) {//   visitedUrl.find( surl )=visitedUrl.end() 表示visitedUrl中没有sur1
                visitedUrl.insert(surl);
                ofile << surl << endl;
                hrefUrl.push(surl);//将sur1插到队列末
            }
            pos = strstr(pos, tag);
            delete[] url; 
        }
    }
    ofile << endl << endl;
    ofile.close();
    tag = "<img ";
    const char* att1 = "src=\"";//规定图像的 URL。
    const char* att2 = "lazy_src=\"";
    const char* pos0 = strstr(p, tag);
    while (pos0){
        pos0 += strlen(tag);
        const char* pos2 = strstr(pos0, att2);
        if (!pos2 || pos2 > strstr(pos0, ">")){
            pos = strstr(pos0, att1);//非延迟加载项
            if (!pos)
            {
                pos0 = strstr(att1, tag);
                continue;
            }
            else
            {
                pos = pos + strlen(att1);
            }
        }else {
            pos = pos2 + strlen(att2);
        }

        const char* nextQ = strstr(pos, "\"");
        if (nextQ){
            char* url = new char[nextQ - pos + 1];
            sscanf(pos, "%[^\"]", url);
            cout << url << endl;
            string imgUrl = url;
            if (visitedImg.find(imgUrl) == visitedImg.end())
            {
                visitedImg.insert(imgUrl);
                imgurls.push_back(imgUrl);//imgurls作为模板队列,push_back的参数由单个字符变成string类型
            }
            pos0 = strstr(pos0, tag);
            delete[] url;
        }
    }
    cout << "网页请求成功!" << endl;
}

//把URL转化为文件名
string ToFileName(const string& url) {
    string fileName;
    fileName.resize(url.size());
    int k = 0;
    for (int i = 0; i < (int)url.size(); i++) {
        char ch = url[i];
        if (ch != '\\' && ch != '/' && ch != ':' && ch != '*' && ch != '?' && ch != '"' && ch != '<' && ch != '>' && ch != '|')
            fileName[k++] = ch;
    }
    return fileName.substr(0, k) + ".txt";//从第0位开始,长度为k的字符串 + .txt
}

//下载图片到img文件夹
void DownLoadImg(vector<string>& imgurls, const string& url) {

    //生成保存该url下图片的文件夹
    string foldname = ToFileName(url);
    foldname = "./img/" + foldname;
    if (!CreateDirectoryA(foldname.c_str(), NULL))
        cout << "不能创建文件:" << foldname << endl;
    char* image;
    int byteRead;
    for (int i = 0; i < imgurls.size(); i++)
    {
        //判断是否为图片
        string str = imgurls[i];
        int pos = str.find_last_of(".");
        if (pos == string::npos)
            continue;
        else
        {
            string ext = str.substr(pos + 1, str.size() - pos - 1);
            if (ext != "bmp" && ext != "jpg" && ext != "jpeg" && ext != "gif" && ext != "png")
                continue;
        }
        //下载其中的内容
        if (GetHttpResponse(imgurls[i], image, byteRead))
        {
            if (strlen(image) == 0)
            {
                continue;
            }
            const char* p = image;
            const char* pos = strstr(p, "\r\n\r\n") + strlen("\r\n\r\n");
            int index = imgurls[i].find_last_of("/");
            if (index != string::npos)
            {
                string imgname = imgurls[i].substr(index, imgurls[i].size());
                ofstream ofile(foldname + imgname, ios::binary);
                if (!ofile.is_open())
                    continue;
                cout << g_ImgCnt++ << foldname + imgname << endl;
                ofile.write(pos, byteRead - (pos - p));
                ofile.close();
            }
            free(image);
        }
    }
}

//广度遍历
void BFS(const string& url) {
    char* response;
    int bytes;
    // 获取网页的相应,放入response中。
    if (!GetHttpResponse(url, response, bytes)) {
        cout << "网页获取错误!." << endl;
        return;
    }
    string httpResponse = response;
    free(response);
    string filename = ToFileName(url);
    ofstream ofile("./html/" + filename);
    if (ofile.is_open()) {
        ofile << httpResponse << endl; // 保存该网页的文本内容
        ofile.close();
    }
    vector<string> imgurls;
    HTMLParse(httpResponse, imgurls, url);//解析该网页的所有图片链接,放入imgurls里面
    DownLoadImg(imgurls, url);
}
//文件内容搜索 
void Fstring(string& urlar) {
    int i = 0;
    cout << "请输入需要查找的字符串";
    string defo, urltxt;
    cin >> defo;
    ifstream ifile;
    do {
        if (urlar[i] == NULL) {
            cout << "没有找到所要内容";
            ifile.close();
            return;
        }
        ifstream ifile("./html/" + urlar[i++]);
        ifile >> urltxt;
    } while (urltxt.find(defo) == string::npos);//字符串对比 
    cout << "搜索内容位于:" << urlar[i - 1];
    ifile.close();
}

int main(){
    //初始化socket,用于tcp网络连接
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        return 0;
    }
    // 创建文件夹,保存图片和网页文本文件
    CreateDirectoryA("./img", 0);
    CreateDirectoryA("./html", 0);
    string urlStart = "http://hao.360.cn/meinvdaohang.html";
    //cout << "请输入url";
    //cin >> urlStart;
    BFS(urlStart);
    string urlar[2000];
    int i = 0;
    // 访问过的网址保存起来
    visitedUrl.insert(urlStart);
    while (hrefUrl.size() != 0) {
        string url = hrefUrl.front();  // 从队列的最开始取出一个网址
        cout << url << endl;
        BFS(url);                      // 遍历提取出来的那个网页,找它里面的超链接网页放入hrefUrl,下载它里面的文本,图片
        urlar[i++] = ToFileName(url);
        hrefUrl.pop();                 // 遍历完之后,删除这个网址
    }
    int p;
    cout << "是否需要查找内容?(1,查找,2退出):";
    cin >> p;
    if (p == 1)
        Fstring(*urlar);
    WSACleanup();//解除socket

    return 0;
}
c++
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
求解c++爬虫,这个代码为何老是(hash_set)错误?

老是编译错误好难呀,越改越错只好让大佬们帮忙看看了!用的是vs2019 报错 ``` E0035 #error 指令: <hash_set> is deprecated and will be REMOVED. Please use <unordered_set>. You can define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS to acknowledge that you have received this warning. c爬虫 C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.24.28314\include\hash_set 21 错误 C1189 #error: <hash_set> is deprecated and will be REMOVED. Please use <unordered_set>. You can define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS to acknowledge that you have received this warning. c爬虫 C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.24.28314\include\hash_set 22 ``` 设计爬虫程序并将爬取到内容存放在文件中。 要求: 1. 可将指定URL及其级联的三级网页下面的所有URL及相应内容找出来。 2. 以BFS或者DFS策略搜索级联页面,设计相应的数据结构与文件,将URL与页面内容(文本形式)保存。 3. 提供简单的搜索引擎功能(给定字符串,如果在级联的三级网页下面下存在,则返回相应的URL) ``` #include <string> #include <iostream> #include <fstream> #include <vector> #include "winsock2.h" #include <time.h> #include <queue> #include <hash_set> #pragma comment(lib, "ws2_32.lib") using namespace std; #define DEFAULT_PAGE_BUF_SIZE 1048576 queue<string> hrefUrl; hash_set<string> visitedUrl; hash_set<string> visitedImg; int g_ImgCnt = 1;//图片编号 bool ParseURL(const string& url, string& host, string& resource) {//解析URL,解析出主机名,资源名 if (strlen(url.c_str()) > 2000) { return false; } const char* pos = strstr(url.c_str(), "http://"); if (pos == NULL) pos = url.c_str(); else pos += strlen("http://"); char pHost[100]; char pResource[2000]; if (strstr(pos, "/") == 0) { pResource[0] = '/'; pResource[1] = 0; strcpy(pHost, pos); } else sscanf(pos, "%[^/]%s", pHost, pResource); host = pHost; resource = pResource; return true; } //使用Get请求,得到响应 bool GetHttpResponse(const string& url, char*& response, int& bytesRead) { string host, resource; if (!ParseURL(url, host, resource)) { cout << "获取get请求失败" << endl; return false; } //建立socket struct hostent* hp = gethostbyname(host.c_str()); if (hp == NULL) { cout << " host 地址错误" << endl; return false; } SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == -1 || sock == -2) { cout << " sock创建失败" << endl; return false; } //建立服务器地址 SOCKADDR_IN sa; sa.sin_family = AF_INET; sa.sin_port = htons(80); memcpy(&sa.sin_addr, hp->h_addr, 4); //建立连接 if (0 != connect(sock, (SOCKADDR*)&sa, sizeof(sa))) { cout << "不能连接: " << url << endl; closesocket(sock); return false; }; //准备发送数据 string request = "GET " + resource + " HTTP/1.1\r\nHost:" + host + "\r\nConnection:Close\r\n\r\n";//字符串拼接 //发送数据 if (SOCKET_ERROR == send(sock, request.c_str(), request.size(), 0)) {//request.size()=strlen(request.c_str) cout << "send error" << endl; closesocket(sock); return false; } //接收数据 int m_nContentLength = DEFAULT_PAGE_BUF_SIZE; char* pageBuf = (char*)malloc(m_nContentLength); memset(pageBuf, 0, m_nContentLength); bytesRead = 0; int ret = 1; while (ret > 0) { ret = recv(sock, pageBuf + bytesRead, m_nContentLength - bytesRead, 0); if (ret > 0){ bytesRead += ret; } if (m_nContentLength - bytesRead < 100){ m_nContentLength *= 2; pageBuf = (char*)realloc(pageBuf, m_nContentLength); } cout << ret << " "; } cout << endl; pageBuf[bytesRead] = '\0'; response = pageBuf; closesocket(sock); return true; } //提取所有的URL以及图片URL void HTMLParse(string& htmlResponse, vector<string>& imgurls, const string& host) { const char* p = htmlResponse.c_str(); const char* tag = "href=\"";//href的内容 就是指要跳转的路由 或 方法 const char* pos = strstr(p, tag); ofstream ofile("url.txt", ios::app);//以追加的方式打开文件 while (pos){ pos += strlen(tag); const char* nextQ = strstr(pos, "\""); if (nextQ){ char* url = new char[nextQ - pos + 1]; sscanf(pos, "%[^\"]", url); string surl = url; // 转换成string类型,可以自动释放内存 if (visitedUrl.find(surl) == visitedUrl.end()) {// visitedUrl.find( surl )=visitedUrl.end() 表示visitedUrl中没有sur1 visitedUrl.insert(surl); ofile << surl << endl; hrefUrl.push(surl);//将sur1插到队列末 } pos = strstr(pos, tag); delete[] url; } } ofile << endl << endl; ofile.close(); tag = "<img "; const char* att1 = "src=\"";//规定图像的 URL。 const char* att2 = "lazy_src=\""; const char* pos0 = strstr(p, tag); while (pos0){ pos0 += strlen(tag); const char* pos2 = strstr(pos0, att2); if (!pos2 || pos2 > strstr(pos0, ">")){ pos = strstr(pos0, att1);//非延迟加载项 if (!pos) { pos0 = strstr(att1, tag); continue; } else { pos = pos + strlen(att1); } }else { pos = pos2 + strlen(att2); } const char* nextQ = strstr(pos, "\""); if (nextQ){ char* url = new char[nextQ - pos + 1]; sscanf(pos, "%[^\"]", url); cout << url << endl; string imgUrl = url; if (visitedImg.find(imgUrl) == visitedImg.end()) { visitedImg.insert(imgUrl); imgurls.push_back(imgUrl);//imgurls作为模板队列,push_back的参数由单个字符变成string类型 } pos0 = strstr(pos0, tag); delete[] url; } } cout << "网页请求成功!" << endl; } //把URL转化为文件名 string ToFileName(const string& url) { string fileName; fileName.resize(url.size()); int k = 0; for (int i = 0; i < (int)url.size(); i++) { char ch = url[i]; if (ch != '\\' && ch != '/' && ch != ':' && ch != '*' && ch != '?' && ch != '"' && ch != '<' && ch != '>' && ch != '|') fileName[k++] = ch; } return fileName.substr(0, k) + ".txt";//从第0位开始,长度为k的字符串 + .txt } //下载图片到img文件夹 void DownLoadImg(vector<string>& imgurls, const string& url) { //生成保存该url下图片的文件夹 string foldname = ToFileName(url); foldname = "./img/" + foldname; if (!CreateDirectoryA(foldname.c_str(), NULL)) cout << "不能创建文件:" << foldname << endl; char* image; int byteRead; for (int i = 0; i < imgurls.size(); i++) { //判断是否为图片 string str = imgurls[i]; int pos = str.find_last_of("."); if (pos == string::npos) continue; else { string ext = str.substr(pos + 1, str.size() - pos - 1); if (ext != "bmp" && ext != "jpg" && ext != "jpeg" && ext != "gif" && ext != "png") continue; } //下载其中的内容 if (GetHttpResponse(imgurls[i], image, byteRead)) { if (strlen(image) == 0) { continue; } const char* p = image; const char* pos = strstr(p, "\r\n\r\n") + strlen("\r\n\r\n"); int index = imgurls[i].find_last_of("/"); if (index != string::npos) { string imgname = imgurls[i].substr(index, imgurls[i].size()); ofstream ofile(foldname + imgname, ios::binary); if (!ofile.is_open()) continue; cout << g_ImgCnt++ << foldname + imgname << endl; ofile.write(pos, byteRead - (pos - p)); ofile.close(); } free(image); } } } //广度遍历 void BFS(const string& url) { char* response; int bytes; // 获取网页的相应,放入response中。 if (!GetHttpResponse(url, response, bytes)) { cout << "网页获取错误!." << endl; return; } string httpResponse = response; free(response); string filename = ToFileName(url); ofstream ofile("./html/" + filename); if (ofile.is_open()) { ofile << httpResponse << endl; // 保存该网页的文本内容 ofile.close(); } vector<string> imgurls; HTMLParse(httpResponse, imgurls, url);//解析该网页的所有图片链接,放入imgurls里面 DownLoadImg(imgurls, url); } //文件内容搜索 void Fstring(string& urlar) { int i = 0; cout << "请输入需要查找的字符串"; string defo, urltxt; cin >> defo; ifstream ifile; do { if (urlar[i] == NULL) { cout << "没有找到所要内容"; ifile.close(); return; } ifstream ifile("./html/" + urlar[i++]); ifile >> urltxt; } while (urltxt.find(defo) == string::npos);//字符串对比 cout << "搜索内容位于:" << urlar[i - 1]; ifile.close(); } int main(){ //初始化socket,用于tcp网络连接 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { return 0; } // 创建文件夹,保存图片和网页文本文件 CreateDirectoryA("./img", 0); CreateDirectoryA("./html", 0); string urlStart = "http://hao.360.cn/meinvdaohang.html"; //cout << "请输入url"; //cin >> urlStart; BFS(urlStart); string urlar[2000]; int i = 0; // 访问过的网址保存起来 visitedUrl.insert(urlStart); while (hrefUrl.size() != 0) { string url = hrefUrl.front(); // 从队列的最开始取出一个网址 cout << url << endl; BFS(url); // 遍历提取出来的那个网页,找它里面的超链接网页放入hrefUrl,下载它里面的文本,图片 urlar[i++] = ToFileName(url); hrefUrl.pop(); // 遍历完之后,删除这个网址 } int p; cout << "是否需要查找内容?(1,查找,2退出):"; cin >> p; if (p == 1) Fstring(*urlar); WSACleanup();//解除socket return 0; } #include <string> #include <iostream> #include <fstream> #include <vector> #include "winsock2.h" #include <time.h> #include <queue> #include <hash_set> #pragma comment(lib, "ws2_32.lib") using namespace std; #define DEFAULT_PAGE_BUF_SIZE 1048576 queue<string> hrefUrl; hash_set<string> visitedUrl; hash_set<string> visitedImg; int g_ImgCnt = 1;//图片编号 bool ParseURL(const string& url, string& host, string& resource) {//解析URL,解析出主机名,资源名 if (strlen(url.c_str()) > 2000) { return false; } const char* pos = strstr(url.c_str(), "http://"); if (pos == NULL) pos = url.c_str(); else pos += strlen("http://"); char pHost[100]; char pResource[2000]; if (strstr(pos, "/") == 0) { pResource[0] = '/'; pResource[1] = 0; strcpy(pHost, pos); } else sscanf(pos, "%[^/]%s", pHost, pResource); host = pHost; resource = pResource; return true; } //使用Get请求,得到响应 bool GetHttpResponse(const string& url, char*& response, int& bytesRead) { string host, resource; if (!ParseURL(url, host, resource)) { cout << "获取get请求失败" << endl; return false; } //建立socket struct hostent* hp = gethostbyname(host.c_str()); if (hp == NULL) { cout << " host 地址错误" << endl; return false; } SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == -1 || sock == -2) { cout << " sock创建失败" << endl; return false; } //建立服务器地址 SOCKADDR_IN sa; sa.sin_family = AF_INET; sa.sin_port = htons(80); memcpy(&sa.sin_addr, hp->h_addr, 4); //建立连接 if (0 != connect(sock, (SOCKADDR*)&sa, sizeof(sa))) { cout << "不能连接: " << url << endl; closesocket(sock); return false; }; //准备发送数据 string request = "GET " + resource + " HTTP/1.1\r\nHost:" + host + "\r\nConnection:Close\r\n\r\n";//字符串拼接 //发送数据 if (SOCKET_ERROR == send(sock, request.c_str(), request.size(), 0)) {//request.size()=strlen(request.c_str) cout << "send error" << endl; closesocket(sock); return false; } //接收数据 int m_nContentLength = DEFAULT_PAGE_BUF_SIZE; char* pageBuf = (char*)malloc(m_nContentLength); memset(pageBuf, 0, m_nContentLength); bytesRead = 0; int ret = 1; while (ret > 0) { ret = recv(sock, pageBuf + bytesRead, m_nContentLength - bytesRead, 0); if (ret > 0){ bytesRead += ret; } if (m_nContentLength - bytesRead < 100){ m_nContentLength *= 2; pageBuf = (char*)realloc(pageBuf, m_nContentLength); } cout << ret << " "; } cout << endl; pageBuf[bytesRead] = '\0'; response = pageBuf; closesocket(sock); return true; } //提取所有的URL以及图片URL void HTMLParse(string& htmlResponse, vector<string>& imgurls, const string& host) { const char* p = htmlResponse.c_str(); const char* tag = "href=\"";//href的内容 就是指要跳转的路由 或 方法 const char* pos = strstr(p, tag); ofstream ofile("url.txt", ios::app);//以追加的方式打开文件 while (pos){ pos += strlen(tag); const char* nextQ = strstr(pos, "\""); if (nextQ){ char* url = new char[nextQ - pos + 1]; sscanf(pos, "%[^\"]", url); string surl = url; // 转换成string类型,可以自动释放内存 if (visitedUrl.find(surl) == visitedUrl.end()) {// visitedUrl.find( surl )=visitedUrl.end() 表示visitedUrl中没有sur1 visitedUrl.insert(surl); ofile << surl << endl; hrefUrl.push(surl);//将sur1插到队列末 } pos = strstr(pos, tag); delete[] url; } } ofile << endl << endl; ofile.close(); tag = "<img "; const char* att1 = "src=\"";//规定图像的 URL。 const char* att2 = "lazy_src=\""; const char* pos0 = strstr(p, tag); while (pos0){ pos0 += strlen(tag); const char* pos2 = strstr(pos0, att2); if (!pos2 || pos2 > strstr(pos0, ">")){ pos = strstr(pos0, att1);//非延迟加载项 if (!pos) { pos0 = strstr(att1, tag); continue; } else { pos = pos + strlen(att1); } }else { pos = pos2 + strlen(att2); } const char* nextQ = strstr(pos, "\""); if (nextQ){ char* url = new char[nextQ - pos + 1]; sscanf(pos, "%[^\"]", url); cout << url << endl; string imgUrl = url; if (visitedImg.find(imgUrl) == visitedImg.end()) { visitedImg.insert(imgUrl); imgurls.push_back(imgUrl);//imgurls作为模板队列,push_back的参数由单个字符变成string类型 } pos0 = strstr(pos0, tag); delete[] url; } } cout << "网页请求成功!" << endl; } //把URL转化为文件名 string ToFileName(const string& url) { string fileName; fileName.resize(url.size()); int k = 0; for (int i = 0; i < (int)url.size(); i++) { char ch = url[i]; if (ch != '\\' && ch != '/' && ch != ':' && ch != '*' && ch != '?' && ch != '"' && ch != '<' && ch != '>' && ch != '|') fileName[k++] = ch; } return fileName.substr(0, k) + ".txt";//从第0位开始,长度为k的字符串 + .txt } //下载图片到img文件夹 void DownLoadImg(vector<string>& imgurls, const string& url) { //生成保存该url下图片的文件夹 string foldname = ToFileName(url); foldname = "./img/" + foldname; if (!CreateDirectoryA(foldname.c_str(), NULL)) cout << "不能创建文件:" << foldname << endl; char* image; int byteRead; for (int i = 0; i < imgurls.size(); i++) { //判断是否为图片 string str = imgurls[i]; int pos = str.find_last_of("."); if (pos == string::npos) continue; else { string ext = str.substr(pos + 1, str.size() - pos - 1); if (ext != "bmp" && ext != "jpg" && ext != "jpeg" && ext != "gif" && ext != "png") continue; } //下载其中的内容 if (GetHttpResponse(imgurls[i], image, byteRead)) { if (strlen(image) == 0) { continue; } const char* p = image; const char* pos = strstr(p, "\r\n\r\n") + strlen("\r\n\r\n"); int index = imgurls[i].find_last_of("/"); if (index != string::npos) { string imgname = imgurls[i].substr(index, imgurls[i].size()); ofstream ofile(foldname + imgname, ios::binary); if (!ofile.is_open()) continue; cout << g_ImgCnt++ << foldname + imgname << endl; ofile.write(pos, byteRead - (pos - p)); ofile.close(); } free(image); } } } //广度遍历 void BFS(const string& url) { char* response; int bytes; // 获取网页的相应,放入response中。 if (!GetHttpResponse(url, response, bytes)) { cout << "网页获取错误!." << endl; return; } string httpResponse = response; free(response); string filename = ToFileName(url); ofstream ofile("./html/" + filename); if (ofile.is_open()) { ofile << httpResponse << endl; // 保存该网页的文本内容 ofile.close(); } vector<string> imgurls; HTMLParse(httpResponse, imgurls, url);//解析该网页的所有图片链接,放入imgurls里面 DownLoadImg(imgurls, url); } //文件内容搜索 void Fstring(string& urlar) { int i = 0; cout << "请输入需要查找的字符串"; string defo, urltxt; cin >> defo; ifstream ifile; do { if (urlar[i] == NULL) { cout << "没有找到所要内容"; ifile.close(); return; } ifstream ifile("./html/" + urlar[i++]); ifile >> urltxt; } while (urltxt.find(defo) == string::npos);//字符串对比 cout << "搜索内容位于:" << urlar[i - 1]; ifile.close(); } int main(){ //初始化socket,用于tcp网络连接 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { return 0; } // 创建文件夹,保存图片和网页文本文件 CreateDirectoryA("./img", 0); CreateDirectoryA("./html", 0); string urlStart = "http://hao.360.cn/meinvdaohang.html"; //cout << "请输入url"; //cin >> urlStart; BFS(urlStart); string urlar[2000]; int i = 0; // 访问过的网址保存起来 visitedUrl.insert(urlStart); while (hrefUrl.size() != 0) { string url = hrefUrl.front(); // 从队列的最开始取出一个网址 cout << url << endl; BFS(url); // 遍历提取出来的那个网页,找它里面的超链接网页放入hrefUrl,下载它里面的文本,图片 urlar[i++] = ToFileName(url); hrefUrl.pop(); // 遍历完之后,删除这个网址 } int p; cout << "是否需要查找内容?(1,查找,2退出):"; cin >> p; if (p == 1) Fstring(*urlar); WSACleanup();//解除socket return 0; } ```

hash_map 自定义hash函数出错

最近在使用hash_map 的时候发现不能自定义hash 函数,只要这样的话,就会出问题,hash_map<const char*, int, hash<const char*>, eqstr> day ;但是如果这样的话就不会有问题了,还望大神求解呀 hash_map<const char*, int> days; #include<iostream> #include<hash_map> #include<string> using namespace std; struct eqstr //定义比较函数,字符串的比较函数需要单独定义 { bool operator()(const char* a1, const char* a2) const { return strcmp(a1,a2)==0; } }; int main() { hash_map<const char*, int, hash<const char*>, eqstr> days; days["january1"] = 31; days["january2"] = 41; days["january3"] = 61; days["january4"] = 51; hash_map<const char*, int, hash<const char*>, eqstr>::iterator ite1=days.begin(); hash_map<const char*, int, hash<const char*>, eqstr>::iterator ite2 = days.end(); for (; ite1 != ite2; ++ite1) { cout << ite1->first << endl; } system("pause"); return 0; } ``` ``` ![图片说明](https://img-ask.csdn.net/upload/201803/18/1521363288_472206.jpg) 。

g_hash_table_insert函数无法正确插入

如题:g_hash_table_insert函数在循环中时,把循环变量作为键的时候无法正确插入,例子是插入10值结果只得到八个。 #include <glib.h> #include <string.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char** argv) { int a,b,c,d; b=2; char outbuf[100]="Columbus"; int i=0; GHashTable* hash = g_hash_table_new(g_int_hash, g_int_equal); for(i=0;i<10;i++){ g_hash_table_insert(hash, &i,outbuf); } g_printf("There are %d keys in the hash\n", g_hash_table_size(hash)); for(i=0;i<10;i++){ g_printf("The %d is %s\n",i, g_hash_table_lookup(hash, &i)); } g_printf("The 2 is %s\n", g_hash_table_lookup(hash, &b)); g_hash_table_destroy(hash); return 0; } 输出的结果是: There are 8 keys in the hash The 0 is Columbus The 1 is Columbus The 2 is Columbus The 3 is Columbus The 4 is Columbus The 5 is Columbus The 6 is Columbus The 7 is Columbus The 8 is Columbus The 9 is Columbus The 2 is (null) 为什么输出的数量是8呢,还有就是第二次查找键为2的值的时候为什么是null而在第一次却有值?

关于intel TBB里的concurrent_hash_map里的find方法

最近用到了intel TBB里的concurrent_hash_map里的bool find( accessor& result, const Key& key ),我在if语句里判断了之后,可是发现好像进去了find函数之后就出不来了,有人直到是为什么吗,跪求大神们赐教。 代码如下: bool ThreadSafeLRUCache<TKey, TValue, THash>:: setTValue(const TKey& key,TValue& value) { HashMapAccessor hashAccessor; printf("enter setvalue\n"); if(!m_map.find(hashAccessor, key)){ printf("return false\n"); return false; } printf("left setvalue"); void *ptemp; // Acquire the lock, but don't block if it is already held std::unique_lock<ListMutex> lock(m_listMutex, std::try_to_lock); if (lock) { printf("enter lock\n"); ptemp = hashAccessor->second.m_value.value; hashAccessor->second.m_value = value; lock.unlock(); printf("left lock\n"); } scalable_free(ptemp); return true; }

关于hash_hmac("MD5", "aa=11&bb=2&c=lai")

![图片说明](https://img-ask.csdn.net/upload/201708/20/1503214664_744015.png) 大家好,我是开发java的,最近遇到一个和php相关的问题,想和大家请教一下 hash_hmac(“MD5”, "a=1&b=data&c=2","私钥") php的这个函数内部是怎么实现的,我目前需要用java做一个签名函数,和php的hash_hmac函数功能一致,网上搜索的都使用不了,所以想知道这个函数底层源码是怎么实现的,这样我就可以用java实现了 搜索到一个阿里的技术文档,内容如下,但是无法使用: public static byte[] encryptHMAC(String data, String secret) throws Exception { byte[] bytes = null; try { SecretKey secretKey = new SecretKeySpec(secret.getBytes(Constants.ENCODE_UTF8), "HmacMD5"); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); bytes = mac.doFinal(data.getBytes(Constants.ENCODE_UTF8)); } catch (Exception gse) { throw new IOException(gse.toString()); } return bytes; } public static String byte2hex(byte[] bytes) { StringBuilder sign = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length() == 1) { sign.append("0"); } sign.append(hex.toUpperCase()); } System.out.println(sign.toString().toLowerCase()); return sign.toString().toLowerCase(); }

关于#include<ext/hash_map>问题

In file included from /usr/include/c++/4.8/ext/hash_map:60:0, from AlgorithmNBM.h:9, from AlgorithmNBM.cpp:10: /usr/include/c++/4.8/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp] #warning \ ^ 求大神相助,急,谢谢!!!

nginx 配置错误 unknown directive "hash"

这是配置文件里的一段 ``` upstream openfire9090{ server 192.168.10.211:9090; server 192.168.10.212:9090; hash $http_x_forwarded_for;//说是这里没有 hash指令? } server { listen 9090; server_name localhost; location / { proxy_pass http://openfire9090; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` # service nginx reload nginx: [emerg] unknown directive "hash" in /usr/local/nginx/conf/nginx.conf:55 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

hash_map,unordered_map以string作为键,插入一千多万条数据后崩溃

代码: #include "stdafx.h" #include"iostream" #include<string> #include<map> #include<sstream> #include<time.h> #include<hash_map> using namespace std; typedef hash_map<string, double> myMap; void CharsAllSequen(myMap &resultMap, string &str, unsigned int n, double defaultVal); void Clear0ForStrQuen(myMap &resultMap); int main() { myMap totalMap; myMap resultMap; string str; CharsAllSequen(resultMap, str, 6, 0.00323223); stringstream ss; for (int i = 0; i < 3000; i++) { myMap::iterator iter; for (iter = resultMap.begin(); iter != resultMap.end(); ++iter) { string strTemp = iter->first; ss.str(""); ss.clear(); ss << "_"; ss << i; strTemp += ss.str(); totalMap.insert(myMap::value_type(strTemp, iter->second + i / 3001.0)); } } return 0; } void Clear0ForStrQuen(myMap &resultMap) { myMap::iterator iter = resultMap.begin(); while (iter!=resultMap.end()) { iter->second = 0; ++iter; } } //获取特定长度,由0,1,2,3四个字符组成的字符串,比如010203,作为key,值设置为defaultVal void CharsAllSequen(myMap &resultMap, string &str, unsigned int n, double defaultVal) { if (n==0) { if (str.size()>0) { resultMap.insert(myMap::value_type(str, defaultVal)); } else { return; } } else { for (int i = 0; i < 4; i++) { stringstream ss; ss << i; string temp = ss.str(); str.append(temp); CharsAllSequen(resultMap, str, n - 1, defaultVal); str = str.substr(0, str.size() - 1); } } } ![图片说明](https://img-ask.csdn.net/upload/201603/30/1459321907_516343.png)

如何在节点js中使用hash_hmac函数

<div class="post-text" itemprop="text"> <p>Recently I have used same code on PHP and it's working fine but when I tried Node Js, it's not working for me. Please check once: </p> <p>PHP </p> <pre><code>$signature = $ACCID . "POST" . strtolower(urlencode($url)).$requestContentBase64String; $hmacsignature = base64_encode(hash_hmac("sha256", $signature, base64_decode($APIKey), true)); </code></pre> <p>NODE CODE : </p> <pre><code>var signature = ACCID+"POST"+encodeURI(url).toLowerCase()+requestContentBase64String; var hmacsignature = base64.encode(crypto.createHmac('sha256', APIKey).update(signature).digest('base64')) </code></pre> <p>Please check what's wrong in this code. </p> </div>

std::map和stdext::hash_map效率问题

近期,接到一个项目,由于需要在程序运行中解析一部分数据,且该部分数据需要向后提供。 所以前同事在设计时使用了stdext::hash~map。 近期在优化该程序,需要将处理能力提高100%,我从IO,队列锁等方面改了一大通,现在效率才提高50%。 所以想问下经验较多的人,map和hash~map效率到底差距有多少(结合我的使用场景)。 1、每个map(hash~map)最多只有100个数据,键值为string std::map<string, MyDataStruct> 键值长度4~30个字符不等,且会存在汉语(会影响效率吗) 2、每个处理逻辑map均会重新构建, 两者插入效率是否差异较大。 3、查询效率,该部分自己已经写代码测试过。两者效率(vs2008+STLport)差异不大,查询10万次hash~map约少个1毫秒。(GetTicketCount()); 由于现在想知道,但是手头没有电脑,没法测试,想问下大家具体的经验。最好是自己做过实验的。

Zend HashTable API:zend_hash_clean()

<div class="post-text" itemprop="text"> <p>I'm new to <strong>Zend</strong> engine and writing an extension. According to <a href="http://www.phpinternalsbook.com/hashtables/hashtable_api.html" rel="nofollow">this page</a> if I want to remove all elements from a hashtable but not actually destroy everything, then I can use the <code>zend_hash_clean()</code> function.</p> <p>However, when I carefully studied this particular function, I realised that the array of buckets is not released as in <code>zend_hash_destroy()</code> even though the destructor is invoked for each bucket element and all the other values are reset <em>(nNumUsed, nNumOfElements, nInteralPointer, etc.)</em>.</p> <p>I highly suspect this is the source of my memory leaks. </p> <p>Please let me know if I'm wrong. Any suggestion or pointer is much appreciated.</p> </div>

nginx ip_hash 负载均衡问题

nginx配置如下: #user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 定义负载均衡服务器列表 upstream svr_balance{ ip_hash; #均衡规则为 根据客户端ip地址的哈希值来分配 server 192.168.78.177; server 202.202.202.167; } server { # 虚拟服务器 http://192.168.78.177:8081 listen 8081; server_name localhost; location / { root /usr/local/nginx/html/; #网站根目录 index index.php index.html index.htm; #定义首页索引文件的名称 } # 定义 http://192.168.78.177:8081/AlienWi/ 访问规则 location /AlienWi/{ proxy_pass http://svr_balance/AlienWi/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } } 不同的客户端(ip地址同一网段,不同网段都有测试)访问http://192.168.78.178:8081/AlienWi/ 为什么总是访问到202.202.202.167, 而没有访问到192.168.78.177呢

SHA1秘密编码,等效于PHP hash_hmac

<div class="post-text" itemprop="text"> <p>I have the following PHP function</p> <pre><code>public function encodePassword($raw, $salt) { return hash_hmac('sha1', $raw . $salt, $this-&gt;secret); } </code></pre> <p>which I need to translate to Go. I found the following example, but it doesn't involve secret key. <a href="https://gobyexample.com/sha1-hashes" rel="nofollow noreferrer">https://gobyexample.com/sha1-hashes</a></p> <p>How can I create a function in Go, that produces exactly same result as PHP's hash_hmac?</p> <blockquote> <p>Update: After Leo's answer, found this resource with hmac examples in many languages: <a href="https://github.com/danharper/hmac-examples" rel="nofollow noreferrer">https://github.com/danharper/hmac-examples</a>. Can be useful to somebody.</p> </blockquote> </div>

如何在PHP上使用hash_hmac()和“SHA256withRSA”?

<div class="post-text" itemprop="text"> <p>I'm trying to get PayPal Webhooks to work with my PHP app. The problem is the hashing algorithm they send via headers, that i must use to verify if the request is valid. </p> <p>When I try to use it, I get this error:</p> <pre><code>hash_hmac(): Unknown hashing algorithm: SHA256withRSA </code></pre> <p>I have tried hash_hmac using just the "sha256" algo and it worked, so I think the problem must be with the one they want me to use.</p> <p>Here is the code I use to process the Webhook:</p> <pre><code>$headers = apache_request_headers(); $body = @file_get_contents('php://input'); $json = json_decode($body); // Concatanate the reqired strings values $sigString = $headers['PAYPAL-TRANSMISSION-ID'].'|'.$headers['PAYPAL-TRANSMISSION-TIME'].'|'.$json-&gt;id.'|'.crc32($body); // Get the certificate file and read the key $pub_key = openssl_pkey_get_public(file_get_contents($headers['PAYPAL-CERT-URL'])); $keyData = openssl_pkey_get_details($pub_key); // check signature if ($headers['PAYPAL-TRANSMISSION-SIG'] != hash_hmac($headers['PAYPAL-AUTH-ALGO'],$sigString,$keyData['key'])) { //invalid } </code></pre> </div>

将PHP中的hash_hmac转换为rails等效项

<div class="post-text" itemprop="text"> <p>I want to convert below PHP hash-code to equivalent ruby or rails code. </p> <pre><code>$publicHash = '3441df0babc2a2dda551d7cd39fb235bc4e09cd1e4556bf261bb49188f548348'; $privateHash = 'e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e'; $content = json_encode( array( 'test' =&gt; 'content' ) ); $hash = hash_hmac('sha256', $content, $privateHash); </code></pre> <p>$content is request params hash. </p> <p>I converted in ROR using, </p> <pre><code>public_hash = '3441df0babc2a2dda551d7cd39fb235bc4e09cd1e4556bf261bb49188f548348' private_hash = 'e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e' content = JSON::encode( paramsString ) hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), private_hash, content) </code></pre> <p>But no luck. Anything I am missing?</p> </div>

运行静态库程序失败(Glib2.48.1)

I have a big problem that confused me many weeks. For project needs, I have to rebuild the Parsefile with statically libraries. I rebuild all dependent libraries statically and modified some cmakelist.txt, and finally I rebuild it successful. But when I run it use command ./Parsefile file.xml . an error occurred and show: (process:30290): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed (process:30290): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed ** GLib:ERROR:/build/glib2.0-7IO_Yw/glib2.0-2.48.1/./glib/gquark.c:60:g_quark_init: assertion failed: (quark_seq_id == 0) Aborted (core dumped) I use gdb to debug it : [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". (process:30307): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed (process:30307): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed ** GLib:ERROR:/build/glib2.0-7IO_Yw/glib2.0-2.48.1/./glib/gquark.c:60:g_quark_init: assertion failed: (quark_seq_id == 0) Program received signal SIGABRT, Aborted. 0x00000000009f8158 in raise () (gdb) bt #0 0x00000000009f8158 in raise () #1 0x00000000009f849a in abort () #2 0x00000000007186fd in g_assertion_message () #3 0x0000000000718796 in g_assertion_message_expr () #4 0x00000000007095cd in g_quark_init () #5 0x0000000000402534 in gobject_init_ctor () #6 0x00000000009ded17 in __libc_csu_init () #7 0x00000000009de31e in generic_start_main () #8 0x00000000009de56a in __libc_start_main () #9 0x0000000000402e59 in _start () Platform information: Linux yangt-MS-7721 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux Distributor ID: Ubuntu Description: Ubuntu 16.04.1 LTS Release: 16.04 Codename: xenial Glib:2.48.1 Hope can get your help, Thx!

Cakephp 3自定义密码列在模型中使用_setPassword方法?

<div class="post-text" itemprop="text"> <p>I have a column in the database with the name <code>PASS</code> that stores the password. It needs to go through the _setPassword method for hashing. but by default it will only go through that method if the column name is <code>password</code>, how do I make <code>PASS</code> to go through that <code>_setPassword</code> method like <code>password</code>?</p> <pre><code>&lt;?php namespace App\Model\Entity; use Cake\ORM\Entity; use Cake\Auth\DefaultPasswordHasher; /** * DefaultUser Entity. */ class DefaultUser extends Entity { /** * Fields that can be mass assigned using newEntity() or patchEntity(). * * @var array */ protected $_accessible = [ 'first_name' =&gt; true, 'last_name' =&gt; true, 'email' =&gt; true, 'PASS' =&gt; true, 'active' =&gt; true, ]; protected function _setPassword($password) { $hasher = new DefaultPasswordHasher(); return $hasher -&gt; hash($password); } } </code></pre> <p>Note: this is cakephp 3.0 code.</p> </div>

PHP5.6 session_start无法写入tmp文件夹(php.ini设置不起作用,ini_set at top)

<div class="post-text" itemprop="text"> <p>I've got an odd issue with sessions in my app. The app is running within docker containers. In essence everything within the Dockerfile's and <code>entrypoint-php.sh</code> files sets things up fine. The php.ini settings for sessions is as follows:</p> <pre><code>session.save_path = /tmp session.use_strict_mode = 1 session.name = K2CLOCATION session.auto_start = 0 session.cache_expire = 240 session.hash_function = sha256 </code></pre> <p>but when I hit the index.php file I get the warning</p> <pre><code>Warning: session_start(): open(/tmp/php/session/1/b/f/sess_1bf0f6fcf5413365a2e5540be2f40b58f0f21b5c16d2214257a5303650649715, O_RDWR) failed: No such file or directory (2) in /var/www/k2c/v03/agent_v07/index.php on line 3 </code></pre> <p>If I then edit the index.php file at the top and add <code>ini_set('session.save_path', '/tmp');</code> above <code>session_start();</code> everything works and I can see the sessions been written.</p> <p>The /tmp/php/session folder has 777 permissions. It was originally owned by root and I attempted to change this to the PHP user (<code>nobody:nobody</code>) but this also had no effect.</p> <p>Other things I have tried:</p> <p>I tried moving the sessions folder to somewhere completely removed from the system tmp folder and this resulted in no change.</p> <p>Drive space is only 9% used.</p> <p><strong>System info</strong>:</p> <p>PHP5.6</p> <p>Running dockerized Centos with Nginx</p> </div>

如何从Pem文件中获取subject_hash(md5)

<div class="post-text" itemprop="text"> <p>I have a pem file that I am trying to get the subject_has for in Go.</p> <p>In command line:</p> <pre><code>wmachs-iphone:platform-tools user$ openssl x509 -noout -subject_hash_old -in ../charles-ssl-proxying-certificate.pem e64b345 </code></pre> <p>I've tried using <a href="https://golang.org/pkg/crypto/md5/" rel="nofollow noreferrer">https://golang.org/pkg/crypto/md5/</a> (no luck, md5 value is different) and <a href="https://godoc.org/github.com/spacemonkeygo/openssl==" rel="nofollow noreferrer">https://godoc.org/github.com/spacemonkeygo/openssl==</a>&gt; slight learning curve that I need help on. I tried loading the Pem file but not sure how to get the MD5 portion: </p> <pre><code>pemfile,_ := openssl.LoadCertificateFromPEM(buf.Bytes()) </code></pre> <p>Any ideas?</p> </div>

php hash_hmac到c# - webresponse“NONCE_NOT_PROVIDED”

<div class="post-text" itemprop="text"> <p>I try to convert a php-code to c#</p> <p>php</p> <pre><code>$apikey='xxx'; $apisecretkey='yyy'; $nonce=time(); $uri='https://website.com/api/getsomething?apikey='.$apikey.'&amp;nonce='.$nonce; $sign=hash_hmac('sha512',$uri,$apisecretkey); $resource = curl_init($uri); curl_setopt($resource, CURLOPT_HTTPHEADER, array('apisign:'.$sign)); $execResult = curl_exec($resource); $obj = ($execResult); </code></pre> <p>and here my C#</p> <pre><code>WebRequest _WebRequest; WebResponse _WebResponse; string apikey = "xxx"; string apisecret = "yyy"; string nonce = ((int)(DateTime.UtcNow - new DateTime(1970,1,1)).TotalSeconds).ToString(); string uri = $"https://website.com/api/getsomething?apikey={apikey}nonce={nonce}"; string _Hash_hmac = Hash_hmac(uri, apisecret); _WebRequest = WebRequest.Create(uri); _WebRequest.Headers["apisign"] = _Hash_hmac; _WebResponse = _WebRequest.GetResponse(); </code></pre> <p>The Code for Hash-hmac I use this <a href="https://stackoverflow.com/questions/12804231/c-sharp-equivalent-to-hash-hmac-in-php">C# equivalent to hash_hmac in PHP</a></p> <p>The Webresponse is always <em>"NONCE_NOT_PROVIDED"</em></p> <p>I checked the outcome from the hash_hmac with the one in php - they are the same. And also try string ToUpper and ToLower.</p> <p>What I am doing wrong?</p> </div>

大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了

大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...

在中国程序员是青春饭吗?

今年,我也32了 ,为了不给大家误导,咨询了猎头、圈内好友,以及年过35岁的几位老程序员……舍了老脸去揭人家伤疤……希望能给大家以帮助,记得帮我点赞哦。 目录: 你以为的人生 一次又一次的伤害 猎头界的真相 如何应对互联网行业的「中年危机」 一、你以为的人生 刚入行时,拿着傲人的工资,想着好好干,以为我们的人生是这样的: 等真到了那一天,你会发现,你的人生很可能是这样的: ...

程序员请照顾好自己,周末病魔差点一套带走我。

程序员在一个周末的时间,得了重病,差点当场去世,还好及时挽救回来了。

ArrayList源码分析(入门篇)

ArrayList源码分析 前言: 写这篇博客的主要原因是,在我上一次参加千牵科技Java实习生面试时,有被面试官问到ArrayList为什么查找的速度较快,插入和删除的速度较慢?当时我回答得不好,很大的一部分原因是因为我没有阅读过ArrayList源码,虽然最后收到Offer了,但我拒绝了,打算寒假学得再深入些再广泛些,下学期开学后再去投递其他更好的公司。为了更加深入理解ArrayList,也为

我以为我学懂了数据结构,直到看了这个导图才发现,我错了

数据结构与算法思维导图

String s = new String(" a ") 到底产生几个对象?

老生常谈的一个梗,到2020了还在争论,你们一天天的,哎哎哎,我不是针对你一个,我是说在座的各位都是人才! 上图红色的这3个箭头,对于通过new产生一个字符串(”宜春”)时,会先去常量池中查找是否已经有了”宜春”对象,如果没有则在常量池中创建一个此字符串对象,然后堆中再创建一个常量池中此”宜春”对象的拷贝对象。 也就是说准确答案是产生了一个或两个对象,如果常量池中原来没有 ”宜春” ,就是两个。...

技术大佬:我去,你写的 switch 语句也太老土了吧

昨天早上通过远程的方式 review 了两名新来同事的代码,大部分代码都写得很漂亮,严谨的同时注释也很到位,这令我非常满意。但当我看到他们当中有一个人写的 switch 语句时,还是忍不住破口大骂:“我擦,小王,你丫写的 switch 语句也太老土了吧!” 来看看小王写的代码吧,看完不要骂我装逼啊。 private static String createPlayer(PlayerTypes p...

和黑客斗争的 6 天!

互联网公司工作,很难避免不和黑客们打交道,我呆过的两家互联网公司,几乎每月每天每分钟都有黑客在公司网站上扫描。有的是寻找 Sql 注入的缺口,有的是寻找线上服务器可能存在的漏洞,大部分都...

讲一个程序员如何副业月赚三万的真实故事

loonggg读完需要3分钟速读仅需 1 分钟大家好,我是你们的校长。我之前讲过,这年头,只要肯动脑,肯行动,程序员凭借自己的技术,赚钱的方式还是有很多种的。仅仅靠在公司出卖自己的劳动时...

上班一个月,后悔当初着急入职的选择了

最近有个老铁,告诉我说,上班一个月,后悔当初着急入职现在公司了。他之前在美图做手机研发,今年美图那边今年也有一波组织优化调整,他是其中一个,在协商离职后,当时捉急找工作上班,因为有房贷供着,不能没有收入来源。所以匆忙选了一家公司,实际上是一个大型外包公司,主要派遣给其他手机厂商做外包项目。**当时承诺待遇还不错,所以就立马入职去上班了。但是后面入职后,发现薪酬待遇这块并不是HR所说那样,那个HR自...

女程序员,为什么比男程序员少???

昨天看到一档综艺节目,讨论了两个话题:(1)中国学生的数学成绩,平均下来看,会比国外好?为什么?(2)男生的数学成绩,平均下来看,会比女生好?为什么?同时,我又联想到了一个技术圈经常讨...

副业收入是我做程序媛的3倍,工作外的B面人生是怎样的?

提到“程序员”,多数人脑海里首先想到的大约是:为人木讷、薪水超高、工作枯燥…… 然而,当离开工作岗位,撕去层层标签,脱下“程序员”这身外套,有的人生动又有趣,马上展现出了完全不同的A/B面人生! 不论是简单的爱好,还是正经的副业,他们都干得同样出色。偶尔,还能和程序员的特质结合,产生奇妙的“化学反应”。 @Charlotte:平日素颜示人,周末美妆博主 大家都以为程序媛也个个不修边幅,但我们也许...

MySQL数据库面试题(2020最新版)

文章目录数据库基础知识为什么要使用数据库什么是SQL?什么是MySQL?数据库三大范式是什么mysql有关权限的表都有哪几个MySQL的binlog有有几种录入格式?分别有什么区别?数据类型mysql有哪些数据类型引擎MySQL存储引擎MyISAM与InnoDB区别MyISAM索引与InnoDB索引的区别?InnoDB引擎的4大特性存储引擎选择索引什么是索引?索引有哪些优缺点?索引使用场景(重点)...

如果你是老板,你会不会踢了这样的员工?

有个好朋友ZS,是技术总监,昨天问我:“有一个老下属,跟了我很多年,做事勤勤恳恳,主动性也很好。但随着公司的发展,他的进步速度,跟不上团队的步伐了,有点...

我入职阿里后,才知道原来简历这么写

私下里,有不少读者问我:“二哥,如何才能写出一份专业的技术简历呢?我总感觉自己写的简历太烂了,所以投了无数份,都石沉大海了。”说实话,我自己好多年没有写过简历了,但我认识的一个同行,他在阿里,给我说了一些他当年写简历的方法论,我感觉太牛逼了,实在是忍不住,就分享了出来,希望能够帮助到你。 01、简历的本质 作为简历的撰写者,你必须要搞清楚一点,简历的本质是什么,它就是为了来销售你的价值主张的。往深...

玩转springboot启动banner定义所得

最近接手了一个springboot项目,不是不熟悉这个框架,启动时打印的信息吸引了我。 这不是我熟悉的常用springboot的打印信息啊,我打开自己的项目: 还真是的,不用默认的感觉也挺高大上的。一时兴起,就去研究了一下源代码,还正是有些收获,稍后我会总结一下。正常情况下做为一个老程序员,是不会对这种小儿科感兴趣的,不就是一个控制台打印嘛。哈哈! 于是出于最初的好奇,研究了项目的源代码。看到

带了6个月的徒弟当了面试官,而身为高级工程师的我天天修Bug......

即将毕业的应届毕业生一枚,现在只拿到了两家offer,但最近听到一些消息,其中一个offer,我这个组据说客户很少,很有可能整组被裁掉。 想问大家: 如果我刚入职这个组就被裁了怎么办呢? 大家都是什么时候知道自己要被裁了的? 面试软技能指导: BQ/Project/Resume 试听内容: 除了刷题,还有哪些技能是拿到offer不可或缺的要素 如何提升面试软实力:简历, 行为面试,沟通能...

离职半年了,老东家又发 offer,回不回?

有小伙伴问松哥这个问题,他在上海某公司,在离职了几个月后,前公司的领导联系到他,希望他能够返聘回去,他很纠结要不要回去? 俗话说好马不吃回头草,但是这个小伙伴既然感到纠结了,我觉得至少说明了两个问题:1.曾经的公司还不错;2.现在的日子也不是很如意。否则应该就不会纠结了。 老实说,松哥之前也有过类似的经历,今天就来和小伙伴们聊聊回头草到底吃不吃。 首先一个基本观点,就是离职了也没必要和老东家弄的苦...

男生更看重女生的身材脸蛋,还是思想?

往往,我们看不进去大段大段的逻辑。深刻的哲理,往往短而精悍,一阵见血。问:产品经理挺漂亮的,有点心动,但不知道合不合得来。男生更看重女生的身材脸蛋,还是...

为什么程序员做外包会被瞧不起?

二哥,有个事想询问下您的意见,您觉得应届生值得去外包吗?公司虽然挺大的,中xx,但待遇感觉挺低,马上要报到,挺纠结的。

当HR压你价,说你只值7K,你该怎么回答?

当HR压你价,说你只值7K时,你可以流畅地回答,记住,是流畅,不能犹豫。 礼貌地说:“7K是吗?了解了。嗯~其实我对贵司的面试官印象很好。只不过,现在我的手头上已经有一份11K的offer。来面试,主要也是自己对贵司挺有兴趣的,所以过来看看……”(未完) 这段话主要是陪HR互诈的同时,从公司兴趣,公司职员印象上,都给予对方正面的肯定,既能提升HR的好感度,又能让谈判气氛融洽,为后面的发挥留足空间。...

面试:第十六章:Java中级开发(16k)

HashMap底层实现原理,红黑树,B+树,B树的结构原理 Spring的AOP和IOC是什么?它们常见的使用场景有哪些?Spring事务,事务的属性,传播行为,数据库隔离级别 Spring和SpringMVC,MyBatis以及SpringBoot的注解分别有哪些?SpringMVC的工作原理,SpringBoot框架的优点,MyBatis框架的优点 SpringCould组件有哪些,他们...

面试阿里p7,被按在地上摩擦,鬼知道我经历了什么?

面试阿里p7被问到的问题(当时我只知道第一个):@Conditional是做什么的?@Conditional多个条件是什么逻辑关系?条件判断在什么时候执...

终于懂了TCP和UDP协议区别

终于懂了TCP和UDP协议区别

你打算用Java 8一辈子都不打算升级到Java 14,真香

我们程序员应该抱着尝鲜、猎奇的心态,否则就容易固步自封,技术停滞不前。

无代码时代来临,程序员如何保住饭碗?

编程语言层出不穷,从最初的机器语言到如今2500种以上的高级语言,程序员们大呼“学到头秃”。程序员一边面临编程语言不断推陈出新,一边面临由于许多代码已存在,程序员编写新应用程序时存在重复“搬砖”的现象。 无代码/低代码编程应运而生。无代码/低代码是一种创建应用的方法,它可以让开发者使用最少的编码知识来快速开发应用程序。开发者通过图形界面中,可视化建模来组装和配置应用程序。这样一来,开发者直...

面试了一个 31 岁程序员,让我有所触动,30岁以上的程序员该何去何从?

最近面试了一个31岁8年经验的程序猿,让我有点感慨,大龄程序猿该何去何从。

大三实习生,字节跳动面经分享,已拿Offer

说实话,自己的算法,我一个不会,太难了吧

程序员垃圾简历长什么样?

已经连续五年参加大厂校招、社招的技术面试工作,简历看的不下于万份 这篇文章会用实例告诉你,什么是差的程序员简历! 疫情快要结束了,各个公司也都开始春招了,作为即将红遍大江南北的新晋UP主,那当然要为小伙伴们做点事(手动狗头)。 就在公众号里公开征简历,义务帮大家看,并一一点评。《启舰:春招在即,义务帮大家看看简历吧》 一石激起千层浪,三天收到两百多封简历。 花光了两个星期的所有空闲时...

《经典算法案例》01-08:如何使用质数设计扫雷(Minesweeper)游戏

我们都玩过Windows操作系统中的经典游戏扫雷(Minesweeper),如果把质数当作一颗雷,那么,表格中红色的数字哪些是雷(质数)?您能找出多少个呢?文中用列表的方式罗列了10000以内的自然数、质数(素数),6的倍数等,方便大家观察质数的分布规律及特性,以便对算法求解有指导意义。另外,判断质数是初学算法,理解算法重要性的一个非常好的案例。

立即提问
相关内容推荐