0

这是我拥有的链接,其中包含大量数据,其格式如下:

“ X = 地图编号:Y = 地图名称”

例子

《10000:蘑菇园》。

在 vc++ .net 中,我想编写一个连接到该链接的函数,在数据中搜索一个数字(比如说 10000),然后获取数字旁边的名称(这将是 Mushroom Park),然后将名称放入一个字符串。

下面的代码是我已经拥有的,如果所有数据都在一个 .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;}
4

1 回答 1

1

我以前没有使用过 vc++,但我相信您正在寻找 WebRequest 类来获取数据。Microsoft 有一个关于提出此类请求的教程。它使用流阅读器,因此 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
     }
}
于 2012-07-16T12:58:35.313 回答