HttpWebResponse.GetResponseHeader メソッド
応答で返されたヘッダーの内容を取得します。
Public Function GetResponseHeader( _
ByVal headerName As String _) As String
[C#]
public string GetResponseHeader(stringheaderName);
[C++]
public: String* GetResponseHeader(String* headerName);
[JScript]
public function GetResponseHeader(
headerName : String) : String;
パラメータ
- headerName
返されるヘッダー値。
戻り値
指定したヘッダーの内容。
例外
例外の種類 | 条件 |
---|---|
ObjectDisposedException | 現在のインスタンスは破棄されています。 |
解説
GetResponseHeader を使用して、特定のヘッダーの内容を取得します。どのヘッダーを返すかを指定する必要があります。
使用例
[Visual Basic, C#, C++] この例では、Web 要求を作成し、応答を問い合わせます。サイトが認証を必要とする場合、この例では、チャレンジ文字列で応答します。この文字列は、 GetResponseHeader を使用して抽出されます。
Public Shared Sub GetPage(url As [String])
Try
Dim ourUri As New Uri(url)
' Creates an HttpWebRequest for the specified URL.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(ourUri), HttpWebRequest)
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Console.WriteLine(ControlChars.NewLine + "The server did not issue any challenge. Please try again with a protected resource URL.")
' Releases the resources of the response.
myHttpWebResponse.Close()
Catch e As WebException
Dim response As HttpWebResponse = CType(e.Response, HttpWebResponse)
If Not (response Is Nothing) Then
If response.StatusCode = HttpStatusCode.Unauthorized Then
Dim challenge As String = Nothing
challenge = response.GetResponseHeader("WWW-Authenticate")
If Not (challenge Is Nothing) Then
Console.WriteLine(ControlChars.NewLine + "The following challenge was raised by the server:{0}", challenge)
End If
Else
Console.WriteLine(ControlChars.NewLine + "The following exception was raised : {0}", e.Message)
End If
Else
Console.WriteLine(ControlChars.NewLine + "Response Received from server was null")
End If
Catch e As Exception
Console.WriteLine(ControlChars.NewLine + "The following exception was raised : {0}", e.Message)
End Try
End Sub
[C#]
public static void GetPage(String url)
{
try
{
Uri ourUri = new Uri(url);
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ourUri);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe server did not issue any challenge. Please try again with a protected resource URL.");
// Releases the resources of the response.
myHttpWebResponse.Close();
}
catch(WebException e)
{
HttpWebResponse response = (HttpWebResponse)e.Response;
if (response != null)
{
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
string challenge = null;
challenge= response.GetResponseHeader("WWW-Authenticate");
if (challenge != null)
Console.WriteLine("\nThe following challenge was raised by the server:{0}",challenge);
}
else
Console.WriteLine("\nThe following WebException was raised : {0}",e.Message);
}
else
Console.WriteLine("\nResponse Received from server was null");
}
catch(Exception e)
{
Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
}
}
}
[C++]
void GetPage(String* url) {
try {
Uri* ourUri = new Uri(url);
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest* myHttpWebRequest =
dynamic_cast<HttpWebRequest*>(WebRequest::Create(ourUri));
HttpWebResponse* myHttpWebResponse =
dynamic_cast<HttpWebResponse*>(myHttpWebRequest->GetResponse());
Console::WriteLine(S"\nThe server did not issue any challenge. Please try again with a protected resource URL.");
// Releases the resources of the response.
myHttpWebResponse->Close();
} catch (WebException* e) {
HttpWebResponse* response = dynamic_cast<HttpWebResponse*>(e->Response);
if (response != 0) {
if (response->StatusCode == HttpStatusCode::Unauthorized) {
String* challenge = 0;
challenge= response->GetResponseHeader(S"WWW-Authenticate");
if (challenge != 0)
Console::WriteLine(S"\nThe following challenge was raised by the server: {0}",
challenge);
} else
Console::WriteLine(S"\nThe following WebException was raised : {0}",
e->Message);
} else
Console::WriteLine(S"\nResponse Received from server was 0");
} catch (Exception* e) {
Console::WriteLine(S"\nThe following Exception was raised : {0}",
e->Message);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard