开发者

Unable to cast object of type 'System.Net.HttpWebRequest' to type 'System.Net.FileWebRequest'

I try download file from server with FileWebRequest. But I get error:

Method on download is here:

public string HttpFileGetReq(Uri uri, int reqTimeout, Encoding encoding)
{
    try
    {
        string stringResponse;
        var req = (FileWebRequest)WebRequest.Create(uri);
        req.Timeout = reqTimeout;
        req.Method = WebRequestMethods.File.DownloadFile;

        var res = (FileWebResponse)req.GetResponse();

        //using (var receiveStream = res.GetResponseStream())
        //using (var readStream = new StreamReader(receiveStream,encoding))
        //{
        //    stringResponse = readStream.ReadToEnd();
        //}

        return stringResponse="0K";
    }
    catch (WebException webException)
    {
        throw webException;
    }
}

Usage is here:

public dynamic LoadRoomMsg(IAccount account, string roomId)
{
    try
    {
        string uri = string.Format("http://www-pokec.azet.sk/_s/chat/nacitajPrispevky.php?{0}&lok={1}&lastMsg=0&pub=0&prv=0&r=1295633087203&changeroom=1" , account.SessionId, roomId);

        var htmlStringResult = HttpFileGetReq(new Uri(uri), ReqTimeout, EncodingType);

        //var htmlStringResult = _httpReq.HttpGetReq(new Uri(string.Format("{0}{1}?{2}&am开发者_JAVA百科p;lok=", PokecUrl.RoomMsg,account.SessionId,roomId)),
        //    ReqTimeout, account.Cookies, EncodingType);
        if (!string.IsNullOrEmpty(htmlStringResult))
        {
            return true;
        }
        return false;
    }
    catch (Exception exception)
    {
        throw exception;
    }
}

URL on file is here.

I would like read this file to string variable, that’s all. If anyone have some time and can help me I would be very glad to him.


Your URL (http://...) will produce a HttpWebRequest. You can check with the debugger.

Form MSDN:

The FileWebRequest class implements the WebRequest abstract base class for Uniform Resource Identifiers (URIs) that use the file:// scheme to request local files.

Note the file:// and local files in there.

Tip: Just use the WebClient class.


Rather than implement your own web streams allow the .NET framework to do it all for you with WebClient, for example:

string uri = string.Format(
    "http://www-pokec.azet.sk/_s/chat/nacitajPrispevky.php?{0}&lok={1}&lastMsg=0&pub=0&prv=0&r=1295633087203&changeroom=1",
    account.SessionId,
    roomId);

System.Net.WebClient wc = new System.Net.WebClient();
string webData = wc.DownloadString(uri);

...parse the webdata response here...

Looking at the response from the URL you posted:

{"reason":0}

parsing that should be a simple task with a little string manipulation.


Change FileWebRequest and FileWebResponse to HttpWebRequest and HttpWebResponse.

It doesn't matter that what you're downloading may be a file; as far as the .NET Framework is concerned, you're just retrieving a page from a website.


FileWebRequest is for file:// protocols. Since you're using an http:// url, you want to use HttpWebRequest.

public string HttpFileGetReq(Uri uri, int reqTimeout, Encoding encoding)
{
    string stringResponse;
    var req = (HttpWebRequest)WebRequest.Create(uri);

    req.Timeout = reqTimeout;

    var res = (HttpWebResponse)req.GetResponse();

    using (var receiveStream = res.GetResponseStream())
    {
        using (var readStream = new StreamReader(receiveStream,encoding))
        {
            return readStream.ReadToEnd();
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜