Materials
Materials

Reputation: 7

Error Message as "This stream does not support seek operations"

I am try to open from below code using webRequest in C# Code.I have tried below code,but I am getting error message "This stream does not support seek operations". Please check this below code and advise how to do this.

    private static void DownloadCurrent() {
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://einvoicing
        .internal.cleartax.co/v2/eInvoice/download?
        irns=11eaf48a909dda520db27ff35804d4b42df2c3b6285afee8df586cc4dbd10541");
    webRequest.Method = "GET";
    webRequest.ContentType = "application/pdf";
    webRequest.Headers.Add("owner_id", "78c6beda-54a2-11ea-b064-0af3f8b02c24");
    webRequest.Headers.Add("gstin", "29AAFCD5862R000");
    webRequest.Timeout = 3000;
    webRequest.BeginGetResponse(new AsyncCallback(PlayResponeAsync), webRequest);
}

private static void PlayResponeAsync(IAsyncResult asyncResult) {
    long total = 0;
    long received = 0;
    HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;

    try {
        using (HttpWebResponse webResponse = 
            (HttpWebResponse)webRequest.EndGetResponse(asyncResult)) {
            byte[] buffer = new byte[1024];

            FileStream fileStream = File.OpenWrite("[file name to write]");
            using (Stream input = webResponse.GetResponseStream()) {
                total = input.Length;

                int size = input.Read(buffer, 0, buffer.Length);
                while (size > 0) {
                    fileStream.Write(buffer, 0, size);
                    received += size;

                    size = input.Read(buffer, 0, buffer.Length);
                }
            }

            fileStream.Flush();
            fileStream.Close();
        }
    }
    catch (Exception ex) { }
}

Upvotes: 0

Views: 586

Answers (1)

Fildor
Fildor

Reputation: 16158

To suggest a switch to HttpClient if possible:

I left out the header-configs for brevity, but it's definitely possible to do.

static string url = "https://einvoicing.internal.cleartax.co/v2/eInvoice/download?irns=11eaf48a909dda520db27ff35804d4b42df2c3b6285afee8df586cc4dbd10541";

static HttpClient client = new HttpClient(); // Use same instance over app lifetime!

static async Task DownloadCurrentAsync()
{
     // config headers here or during instance creation
     HttpResponseMessage response = await client.GetAsync(url);
     response.EnsureSuccessStatusCode(); // <= Will throw if unsuccessful
     using (FileStream fileStream = new FileStream("[file name to write]", FileMode.Create, FileAccess.Write, FileShare.None))
     {
         //copy the content from response to filestream
         await response.Content.CopyToAsync(fileStream);
     }
}

Note that this is now TAP, not the legacy Async pattern (APM).

Some additional considerations:

  • For resilience, I'd have a look into using "Polly" here. (I am not affiliated)
  • The client instance should probably be injected through IHttpClientFactory.

Addendum:

If the rest of your app is using exclusively APM, then you could have a look into Interop with Other Asynchronous Patterns and Types

Upvotes: 2

Related Questions