如何解析webresponse并识别是图像还是xml?

时间:2009-12-30 18:01:58

标签: vb.net httpwebresponse

我有一个php脚本,如果用户有权下载并将图像映像发送到webbrowser,但是如果用户没有权限,则会显示带有信息的xml。

phpcode:

if($this->can_download($user->id)){

$id = int_escape($_GET['id']);
$image = Image::by_id($id);

if(!is_null($image)) {
    $page->set_mode("data");

    $page->set_type($image->get_mime_type());
    $file = $image->get_image_filename();

    $page->set_data(file_get_contents($file));

    //WE UPDATE THAT THE USER HAS DOWNLOADED A FILE.
    $this->update_database($user->id, "download");
} else {
    $xml = "<status>\n";
    $xml .= "<info code=\"145\" message=\"Image does not exists.\"/>\n";
    $xml .= "</status>";

    $page->set_type("application/xml");
    $page->set_data($xml);
} 
}else {
    $xml = "<status>\n";
    $xml .= "<info code=\"145\" message=\"Yor has reached your daily limit.\"/>\n";
    $xml .= "</status>";

    $page->set_type("application/xml");
    $page->set_data($xml);
}

如何识别vb.net的响应类型以保存图像(如果是,或者将xml发送给解析器使用它?)

这是我的vb代码:

Dim PerformedUserUrl As String = UserUrl & "?username=" & username & _
                                             "&password=" & password

Dim WebRequest As HttpWebRequest = HttpWebRequest.Create(PerformedUserUrl)

WebRequest.Method = "GET"

Dim webResponse As HttpWebResponse = Nothing
Dim stReader As StreamReader = Nothing

Dim response as string

Try
    webResponse = WebRequest.GetResponse()
    stReader = New StreamReader(webResponse.GetResponseStream())

    response stReader.ReadToEnd()

Catch ex As Exception
    MsgBox(ex.Message)
End Try

Return response

1 个答案:

答案 0 :(得分:1)

检查ContentType财产怎么样?

如果返回“application / xml”,则可能是XML。否则为图像(基于您提供的服务器端代码)