如何在原始网页上搜索和获取文本? VC ++?

时间:2012-07-16 05:01:32

标签: .net winforms visual-c++

这是我所拥有的link,其中包含大量数据,其格式为:

“X = MapNumber:Y = MapName”

示例

“10000:蘑菇公园”。

在vc ++ .net中,我想编写一个连接到该链接的函数,在数据中搜索一个数字(比方说10000),然后在数字旁边找到名称(这将是蘑菇公园),然后放入将名称转换为字符串。

下面的代码就是我已经存在的,并且如果所有数据都在.txt文件中,我会工作但是我想将此代码转换为连接到上面的链接并最终使其更有效。

感谢。

String^ GrabMapNameById(String^ CurrentStr)//Current Map String{
if(CurrentMapIDRead.ToString() != CurrentMapID.ToString()) //If map has chnaged
{
       try //Try streaming text
       {
          String^ Maps = "Strife.Maps";//map File Name
          StreamReader^ MapDin = File::OpenText("Strife.Maps");//Stream Declar

          String^ str;
          string s;
          int count = 0;
          while ((str = MapDin->ReadLine()) != nullptr) //Loop for every line
          {
             if(str->Contains(CurrentMapID.ToString())) //Untill Map id found
             {
                CurrentMapIDRead = CurrentMapID; //Set Current Map Name
                CurrentStr = str->Replace(CurrentMapIDRead.ToString() , "" );//Replace map id with null characters
                CurrentStr = CurrentStr->Replace(" =" , "" ); //Take out = characters
                break;// kill while
             }
          }
       }

           catch (Exception^ e)
           {
           }
}return CurrentStr;}

1 个答案:

答案 0 :(得分:1)

之前我没有使用过vc ++,但我相信你正在寻找WebRequest类来获取数据。微软有tutorial发出此类请求。它使用流阅读器,因此try块中的代码看起来像这样:

String *sURL = S"http://pastebin.com/raw.php?i=yVyxkWFD";
WebRequest *wrGETURL;
wrGETURL = WebRequest::Create(sURL);
WebProxy *myProxy = new WebProxy(S"myproxy", 80);
myProxy->BypassProxyOnLocal = true;

wrGETURL->Proxy = WebProxy::GetDefaultProxy();

Stream *objStream = wrGETURL->GetResponse()->GetResponseStream();

StreamReader *MapDin = new StreamReader(objStream);

String^ str;
string s;
int count = 0;
while ((str = MapDin->ReadLine()) != nullptr) //Loop for every line
{
     if(str->Contains(CurrentMapID.ToString())) //Untill Map id found
     {
         CurrentMapIDRead = CurrentMapID; //Set Current Map Name
         CurrentStr = str->Replace(CurrentMapIDRead.ToString() , "" );//Replace map id with null characters
         CurrentStr = CurrentStr->Replace(" =" , "" ); //Take out = characters
         break;// kill while
     }
}