HttpWebRequestを使うならこちら
String^ result; String^ url = "ここにURLを書く"; //IDとパスワードを設定するならここで NetworkCredential^ nc = gcnew NetworkCredential(id,password); try{ HttpWebRequest^ request = (HttpWebRequest^)HttpWebRequest::Create(url); request->Credentials=nc; WebResponse^ response = request->GetResponse(); StreamReader^ reader = gcnew StreamReader(response->GetResponseStream()); result = reader->ReadToEnd(); reader->Close(); response->Close(); }catch(System::Net::WebException^ e){ //ここでなんらかのエラー処理をする System::Diagnostics::Debug::WriteLine("エラー:"+ e->Response->ToString()); return ""; }
WebClientを使うなら、こんな感じです。
String^ result; String^ url = "ここにURLを書く"; try{ WebClient^ client = gcnew WebClient(); client->Credentials = nc; client->Encoding = Encoding::UTF8; array^ data =client->DownloadData(url); String^ result =Encoding::UTF8->GetString(data); return result; }catch(System::Net::WebException^ e){ //ここでなんらかのエラー処理をする System::Diagnostics::Debug::WriteLine("エラー:"+ e->Response->ToString()); return ""; }
0 件のコメント:
コメントを投稿