Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:26:01+00:00 2026-06-03T03:26:01+00:00

I’m working on a quick wrapper for the skydrive API in C#, but running

  • 0

I’m working on a quick wrapper for the skydrive API in C#, but running into issues with downloading a file. For the first part of the file, everything comes through fine, but then there start to be differences in the file and shortly thereafter everything becomes null. I’m fairly sure that it’s just me not reading the stream correctly.

This is the code I’m using to download the file:

public const string ApiVersion = "v5.0";
public const string BaseUrl = "https://apis.live.net/" + ApiVersion + "/";

public SkyDriveFile DownloadFile(SkyDriveFile file)
{
    string uri = BaseUrl + file.ID + "/content";
    byte[] contents = GetResponse(uri);
    file.Contents = contents;
    return file;
}

public byte[] GetResponse(string url)
{
    checkToken();
    Uri requestUri = new Uri(url + "?access_token=" + HttpUtility.UrlEncode(token.AccessToken));
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
    request.Method = WebRequestMethods.Http.Get;
    WebResponse response = request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    byte[] contents = new byte[response.ContentLength];
    responseStream.Read(contents, 0, (int)response.ContentLength);
    return contents;
}

This is the image file I’m trying to download

The Correct Image

And this is the image I am getting

Broken Image

These two images lead me to believe that I’m not waiting for the response to finish coming through, because the content-length is the same as the size of the image I’m expecting, but I’m not sure how to make my code wait for the entire response to come through or even really if that’s the approach I need to take.

Here’s my test code in case it’s helpful

[TestMethod]
public void CanUploadAndDownloadFile()
{
    var api = GetApi();
    SkyDriveFolder folder = api.CreateFolder(null, "TestFolder", "Test Folder");
    SkyDriveFile file = api.UploadFile(folder, TestImageFile, "TestImage.png");
    file = api.DownloadFile(file);
    api.DeleteFolder(folder);
    byte[] contents = new byte[new FileInfo(TestImageFile).Length];
    using (FileStream fstream = new FileStream(TestImageFile, FileMode.Open))
    {
        fstream.Read(contents, 0, contents.Length);
    }
    using (FileStream fstream = new FileStream(TestImageFile + "2", FileMode.CreateNew))
    {
        fstream.Write(file.Contents, 0, file.Contents.Length);
    }
    Assert.AreEqual(contents.Length, file.Contents.Length);
    bool sameData = true;
    for (int i = 0; i < contents.Length && sameData; i++)
    {
        sameData = contents[i] == file.Contents[i];
    }
    Assert.IsTrue(sameData);
}

It fails at Assert.IsTrue(sameData);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:26:02+00:00Added an answer on June 3, 2026 at 3:26 am

    This is because you don’t check the return value of responseStream.Read(contents, 0, (int)response.ContentLength);. Read doesn’t ensure that it will read response.ContentLength bytes. Instead it returns the number of bytes read. You can use a loop or stream.CopyTo there.

    Something like this:

    WebResponse response = request.GetResponse();
    MemoryStream m = new MemoryStream();
    response.GetResponseStream().CopyTo(m);
    byte[] contents = m.ToArray();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I'm making a simple page using Google Maps API 3. My first. One marker
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.