0

I have an CSV file in memory that I want to upload to a Web API.

If I save the CSV file to disk and upload it, it gets accepted.

However, I want to avoid the extra work and also make the code cleaner by simply uploading the text I have as a MemoryStream Object (I think that's the correct format?).

The following code works for uploading the file:

string webServiceUrl = "XXX";
string filePath = @"C:\test.csv";
string cred = "YYY";

using (var client = new WebClient()){
    client.Headers.Add("Authorization", "Basic " + cred);
    byte[] rawResponse = client.UploadFile(webServiceUrl, "POST", filePath);   
    Console.WriteLine(System.Text.Encoding.ASCII.GetString(rawResponse));
}  

How would I do if I had a string with all the contents and I want to upload it in the same way without having to save it down to a file?

WebClient.UploadData or WebClient.UploadString perhaps?

Thank you

EDIT:

I tried what you said but by using a local file (in case there was something wrong with the string), but I get the same error.

Here is what I suppose the code would be using your solution

string webServiceUrl = "XXX";
string file = @"C:\test.csv";     
string cred = "YYY";

FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] postArray = r.ReadBytes((int)fs.Length); 

using (var client = new WebClient())
{
    client.Headers.Add("Authorization", "Basic " + cred);
    using (var postStream = client.OpenWrite(webServiceUrl, "POST"))
    {     
          postStream.Write(postArray, 0, postArray.Length); 
    }              
}

Any thoughts?

1
  • Use one of the WebClient UploadData overrides. Pass in the MemoryStream's ToArray as the byte[] parameter. Commented Mar 29, 2017 at 13:00

2 Answers 2

3

Use OpenWrite() from the WebClient.

using (var postStream = client.OpenWrite(endpointUrl))
{
    postStream.Write(memStreamContent, 0, memStream.Length);
}

As documentation mentioned:

The OpenWrite method returns a writable stream that is used to send data to a resource.


Update

Try to set the position of the MemoryStream to 0 before uploading.

memoryStream.Position = 0;

When you copy the file into the MemoryStream, the pointer is moved to the end of the stream, so when you then try to read it, you're getting a null byte instead of your stream data.

MSDN - CopyTo()

Copying begins at the current position in the current stream, and does not reset the position of the destination stream after the copy operation is complete.

Sign up to request clarification or add additional context in comments.

7 Comments

Which Error did you get?
The remote server returned an error: (500) Internal Server Error. It's not more specific than that.
@Roffemon Can you debug the Server? This error message means the Server can't handle Stream's as Input.
Can't, it's a server that I don't have access to. The second code block is very similar to the first, so I must be doing something wrong when handling the stream? I'll try and see if I can solve it, thank you!
@Roffemon Check the position of your MemStream in the debugger before posting to the Server.
|
0

I finally managed to solve it.

First I made a request using CURL that worked. I analyzed the packet data and made an except copy of the packet.

I did a lot of changes, however, the final change was that using the different functions I found online it never closed the packet with a "Last-Boundary" while CURL did.

So by modifying the function, making sure it properly wrote a Last-Boundary it finally worked.

Also, another crucial thing was to set PreAuthenticate to true, the examples online didn't do that.

So, all in all: 1. Make sure that the packet is properly constructed. 2. Make sure you pre authenticate if you need to authenticate.

webrequest.PreAuthenticate = true;
webrequest.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", cred); 
  1. Don't forget to add SSL if using a https (which you probably do if you authenticate):

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

Hope this helps someone.

And thanks for the help earlier!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.