$_POST global empty when uploading a file using HttpWebRequest in C# - c#

I have a c# function that is used to upload a file to a PHP web service. The PHP web service is expecting the following
A POST parameter called UploadFileRequestDto containing some XML data
The File stream
For some odd reason the $_POST parameter contains the UploadFileRequestDto only some of the time. If I look at the contents of
file_get_contents("php://input"))
I can see that the request is comming through as expected with the UploadFileRequestDto included.
Doing a
print_r($_REQUEST)
is returning an empty array.
Can anyone help me with a solution to this problem, my C# function is stipulated below
public string UploadFile(UploadFileRequestDto uploadFileRequestDto,string fileToUpload, string fileUploadEndpoint)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(fileUploadEndpoint);
request.ReadWriteTimeout = 1000 * 60 * 10;
request.Timeout = 1000 * 60 * 10;
request.KeepAlive = false;
var boundary = "B0unD-Ary";
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
var postData = "--" + boundary + "\r\nContent-Disposition: form-data;";
postData += "name=\"UploadFileRequestDto\"\r\n\r\n";
postData += string.Format("{0}\r\n", SerializeUploadfileRequestDto(uploadFileRequestDto));
postData += "--" + boundary + "\r\n";
postData += "--" + boundary + "\r\nContent-Disposition: form-data;name=\"file\";filename=\"" + Path.GetFileName(fileToUpload) + "\"\r\n";
postData += "Content-Type: multipart/form-data\r\n\r\n";
var byteArray = Encoding.UTF8.GetBytes(postData);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
byte[] filedata = null;
using (var reader = new BinaryReader(File.OpenRead(fileToUpload)))
{
filedata = reader.ReadBytes((int)reader.BaseStream.Length);
}
request.ContentLength = byteArray.Length + filedata.Length + boundaryBytes.Length;
request.GetRequestStream().Write(byteArray, 0, byteArray.Length);
request.GetRequestStream().Write(filedata, 0, filedata.Length);
request.GetRequestStream().Write(boundaryBytes, 0, boundaryBytes.Length);
var response = request.GetResponse();
var data = response.GetResponseStream();
var sReader = new StreamReader(data);
var sResponse = sReader.ReadToEnd();
response.Close();
return sResponse.TrimStart(new char[] { '\r', '\n' });
}
catch (Exception ex)
{
LogProvider.Error(string.Format("OzLib.Infrastructure : WebHelper : public string UploadFile(UploadFileRequestDto uploadFileRequestDto, string fileUploadEndpoint) : Exception = {0}", ex.ToString()));
}

Ok I found the problem, the
post_max_size
setting in the php.ini was set to 8M and some of the files I was trying to upload exeeded 8M. Changed this setting to 16M and restarted the PHP service.
When the file size exeeds the limit that was set the $_POST global is empty.

Related

How do I send the final bytes for a Microsoft Graph Upload Session?

I am following the document provided by Microsoft for Uploading Large files into Microsoft Graph.
Everything works great except for the byte array on the final bytes of the file.
https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0
First I create a DriveItem that has a file name and upload an empty byte[].
using (var emptyFileClient = new HttpClient())
{
var url = "https://graph.microsoft.com/v1.0/drives/" + driveID + "/root:/" + sharepoint2013ID + "/" + fileInfo.Name + ":/content";
emptyFileClient.DefaultRequestHeaders.Add("Authorization", bearer);
var emptyFileContent = new ByteArrayContent(new byte[0]);
var emptyFileResponse = emptyFileClient.PutAsync(url, emptyFileContent).Result;
if (emptyFileResponse.StatusCode != System.Net.HttpStatusCode.Created)
{
return false;
}
}
Then I make sure that I have a valid DriveItemID
var client = new RestClient("https://graph.microsoft.com/v1.0/drives/" + driveID + "/root:/" + sharepoint2013ID + "/" + fileInfo.Name);
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", bearer);
IRestResponse response = client.Execute(request);
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
return null;
}
Next I create an UploadSession
//Create UploadSession
var client = new RestClient("https://graph.microsoft.com/v1.0/drives/" + driveID + "/items/" + driveItem.id + "/createUploadSession");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", bearer);
IRestResponse response = client.Execute(request);
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
throw new Exception("Could Not Create Upload Session");
}
This works correctly so now I begin uploading the file.
Again, Everything works correctly until the final byte[].
I have verified that the expected range for the final byte[] matches the byte array that I send.
WebRequest request = null;
using (Stream source = File.OpenRead(fileInfo.FullName))
{
double fileSizeBytes = fileInfo.Length;
int bufferSize = 2048000;
byte[] buffer = new byte[bufferSize];
int bytesRead;
var loopCount = 0;
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
{
double startByte = bufferSize * loopCount;
double endByte = startByte + bytesRead - 1;
var contentRange = "bytes " + startByte + "-" + endByte + "/" + fileSizeBytes;
Console.WriteLine(contentRange);
request = WebRequest.Create(uploadSession.uploadUrl);
request.Headers.Add("Authorization", bearer);
request.Headers.Add("Content-Length", bytesRead.ToString());
request.Headers.Add("Content-Range", contentRange);
request.Method = "PUT";
request.ContentLength = buffer.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(buffer, 0, buffer.Length);
dataStream.Close();
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != System.Net.HttpStatusCode.Accepted)
{
return false;
}
loopCount++;
}
}
I have been testing with large files that exceed the buffer size as to split the upload into multiple requests. You can also test with smaller files if you reduce the buffer size. Any help on this would be appreciated.
Also I am not interested in using the Graph.SDK or any other library, this needs to run from web requests. This is for a hybrid solution where Sharepoint2013 Server will be saving some files in SharepointOnline.
The issue turned out to be that I was setting the content-length header twice and one of them was using the incorrect value. Simply comment out the following line and everything above will work correctly.
//request.ContentLength = buffer.Length;
On the final upload the length is not the full length of the buffer but instead needs to be set to bytesRead.

Upload Excel file to web server

I was trying to upload a file to a web server through UWP c# using filestream but it always gives me an error i.e 405 method not allowed when I try to upload on http://example.com/httpdocs/content. Even for testing purpose I tried uploading on my localhost but still no luck.
Any Help?
Code :
public async Task<bool> Upload(StorageFile fileName)
{
HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();
cts = new CancellationTokenSource();
using (IInputStream fileStream = await fileName.OpenSequentialReadAsync())
{
HttpStreamContent content = new HttpStreamContent(fileStream);
form.Add(content, "premier", fileName.Name);
using (HttpClient client = new HttpClient())
{
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("http://example.com/httpdocs/content")))
{
request.Content = form;
request.Headers.TryAppendWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
HttpResponseMessage response = await client.SendRequestAsync(request).AsTask(cts.Token);
var result = response.Content.ReadAsStringAsync().GetResults();
}
}
}
return true;
}
Hye, after trying much I found this code and it works perfectly. With a little correction that the request will be now sent to an aspx page i.e. http://example.com/abc.aspx. If you wanna send high mb's of excel file data then just change 4096 in Math.Min(4096, (int)fileStream.Length) accordingly
Client Side Code-
public static async Task<string> UploadFileEx(string uploadfile, string
url, string fileFormName, string contenttype, NameValueCollection
querystring, CookieContainer cookies)
{
try
{
if ((fileFormName == null) ||
(fileFormName.Length == 0))
{
fileFormName = "file";
}
if ((contenttype == null) ||
(contenttype.Length == 0))
{
contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
string postdata;
postdata = "?";
if (querystring != null)
{
foreach (string key in querystring.Keys)
{
postdata += key + "=" + querystring.Get(key) + "&";
}
}
Uri uri = new Uri(url + postdata);
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.CookieContainer = cookies;
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
// Build up the post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(fileFormName);
sb.Append("\"; filename=\"");
var sd = sb.Append(Path.GetFileName(uploadfile));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
// Build the trailing boundary string as a byte array
// ensuring the boundary appears on a line by itself
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
// webrequest.ContentLength = length;
webrequest.Headers[HttpRequestHeader.ContentLength] = length.ToString();
//Stream requestStream = webrequest.GetRequestStream();
Stream requestStream = await webrequest.GetRequestStreamAsync();
// Write out our post header
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
// Write out the file contents
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
// Write out the trailing boundary
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
WebResponse response = await webrequest.GetResponseAsync();
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s);
return sr.ReadToEnd();
}
catch (Exception e)
{
e.ToString();
}
return null;
}

Posting tasks to asana through API stopped working

I wrote a program to post tasks to asana through the API and it has been working fine up until this morning, can anyone help me figure out why that is?
this is an example of the JSON string I am sending:
{"workspace":09876543321111,"data": {"assignee":null,"name":"Sample Name","notes":"Sample Noted","due_on":"2015-01-27","projects":"12434567889099","completed":false}}
and I am getting a 400 error: bad request.
this is my code:
string ID = "09876543321111"; //workspace ID
string url = #"https://app.asana.com/api/1.0/workspaces/" + ID + #"/tasks";
Data dat = new Data();
string ProjName = "Test Project";
dat.projects = "1234567890234";
dat.assignee = null;
dat.name = "Sample Name";
dat.notes = "Sample Notes";
dat.due_on = val.requiredBy.Value.ToString("u").Substring(0, 10);
dat.completed = false;
//if task doesnt exist, make one
if (!Tasks.CheckExist(project, dat.projects, dat.name, apiKey, log))
{
string json = JsonConvert.SerializeObject(dat);
string data = "{\"workspace\":" + ID + ",\"data\": " + json + "}";
log.WriteLine(data);
Functions.Post(data, url, apiKey, log);
}
Post function:
//post tasks to asana
public static void Post(string data, string url, string apiKey, StreamWriter log)
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = WebRequestMethods.Http.Post;
req.ContentLength = bytes.Length;
req.ContentType = "application/json";
var authInfo = apiKey + ":";
var encodedAuthInfo = Convert.ToBase64String(
Encoding.Default.GetBytes(authInfo));
req.Headers.Add("Authorization: Basic " + encodedAuthInfo);
req.ContentLength = bytes.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Close();
try
{
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
catch (WebException ex)
{
HttpWebResponse response = ((HttpWebResponse)ex.Response);
string exc = url + " caused a " + (int)response.StatusCode + " error.\n" + response.StatusDescription;
Console.WriteLine(exc);
log.WriteLine(exc);
}
}
EDIT
for anyone who cares I solved the problem by changing string data to:
string data = "{\"data\": " + json + "}";
We recently made a change to return a 400 error if there were unexpected parameters passed at the top level, as (nearly) all API routes only use the parameters passed in under the "data" attribute. In this case (as you correctly determined) the "workspace" attribute at the top level was incorrect - previously we just ignored it, but in an effort to make the API less "surprising" we wanted to be explicit and strict about parameters that could be ignored, as otherwise it could be misleading.

Streamsend API with C#

I cannot seem to get this code to work with Streamsends API. I have been all over the internet and have found very little info pertaining to what I am trying to do. I am trying to upload a file to their server. Can anyone look at this and maybe help me out here?
I keep getting a 500 error from Streamsends servers which they say is within the Streamsend application itself. What is funny is if I take the line near the bottom that is commented out
(//request.ContentType = "multipart/form-data";)
and uncomment it I get a 422 error which is saying invalid data. It seems like there is something wrong with the boundary.
I am fairly new to C# and this is the first time I have worked with Streamsends API so any help would be greatly appreciated.
m_Method = "POST"
m_Action = "uploads"
private string StreamSendResponse(string Path, string FileName)
{
string sReturn = String.Empty;
string sLocation = String.Empty;
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = Encoding.ASCII.GetBytes(Environment.NewLine + "--" + boundary + Environment.NewLine);
Uri addy = new Uri(m_URI + m_Action);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(addy);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = m_Method;
request.KeepAlive = true;
request.Headers.Add("Authorization", "Basic " + Merv.base64Encode(m_ID + ":" + m_Key));
Stream rs = request.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n{1}";
NameValueCollection nvc = new NameValueCollection();
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n";
string header = string.Format(headerTemplate, "data", FileName);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(Path + FileName, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes(Environment.NewLine + "--" + boundary + "--" + Environment.NewLine);
rs.Write(trailer, 0, trailer.Length);
rs.Close();
request.Accept = "application/xml";
//request.ContentType = "multipart/form-data";
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader srResponse = new StreamReader(response.GetResponseStream());
sLocation = response.Headers["Location"];
sReturn = srResponse.ReadToEnd().Trim();
}
catch (WebException exc)
{
StreamReader srResponse = new StreamReader(response.GetResponseStream());
sReturn = srResponse.ReadToEnd().Trim();
sLocation = exc.Message;
}
if (sReturn == String.Empty)
{
sReturn = sLocation;
}
return sReturn;
}

Using HttpWebRequest to POST data/upload image using multipart/form-data

I am trying to use the ImageShack API to upload images. To use it, I am supposed to POST the image using multipart/form-data. I did it like ...
var postData = "";
var req = HttpWebRequest.Create("http://www.imageshack.us/upload_api.php");
req.Method = "POST";
req.ContentType = "multipart/form-data";
postData += "key=my_key_here&";
postData += "type=base64&";
// get base64 data from image
byte[] bytes = File.ReadAllBytes(#"D:\tmp\WpfApplication1\WpfApplication1\Images\Icon128.gif");
string encoded = Convert.ToBase64String(bytes);
postData += "fileupload=" + encoded;
byte[] reqData = Encoding.UTF8.GetBytes(postData);
using (Stream dataStream = req.GetRequestStream())
{
dataStream.Write(reqData, 0, reqData.Length);
}
var res = (HttpWebResponse)req.GetResponse();
var resStream = res.GetResponseStream();
var reader = new StreamReader(resStream);
string resString = reader.ReadToEnd();
txt1.Text = resString;
but ImageShack is complaining that
<links>
<error id="parameter_missing">Sorry, but we've detected that unexpected data is received. Required parameter 'fileupload' is missing or your post is not multipart/form-data</error>
</links>
FileUpload is present and I am using multipart/form-data whats wrong?
UPDATE:
New Code http://pastebin.com/TN6e0CD8
Post data http://pastebin.com/fYE9fsxs
UPDATE 2
i looked at the other question Multipart forms from C# client. modified my code with boundary, removed the expect 100 header still i cant get it working ...
ServicePointManager.Expect100Continue = false;
var boundary = "-----------------------------28520690214962";
var newLine = Environment.NewLine;
var propFormat = boundary + newLine +
"Content-Disposition: form-data; name=\"{0}\"" + newLine + newLine +
"{1}" + newLine + newLine;
var fileHeaderFormat = boundary + newLine +
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + newLine;
var req = (HttpWebRequest)HttpWebRequest.Create("http://jm/php/upload.php");
req.Method = WebRequestMethods.Http.Post;
req.ContentType = "multipart/form-data; boundary=" + boundary;
using (var reqStream = req.GetRequestStream()) {
var reqWriter = new StreamWriter(reqStream);
var tmp = string.Format(propFormat, "str1", "hello world");
reqWriter.Write(tmp);
tmp = string.Format(propFormat, "str2", "hello world 2");
reqWriter.Write(tmp);
reqWriter.Write(boundary + "--");
reqWriter.Flush();
}
var res = req.GetResponse();
using (var resStream = res.GetResponseStream()) {
var reader = new StreamReader(resStream);
txt1.Text = reader.ReadToEnd();
}
I finally got it with the following code ...
var boundary = "------------------------" + DateTime.Now.Ticks;
var newLine = Environment.NewLine;
var propFormat = "--" + boundary + newLine +
"Content-Disposition: form-data; name=\"{0}\"" + newLine + newLine +
"{1}" + newLine;
var fileHeaderFormat = "--" + boundary + newLine +
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + newLine;
var req = (HttpWebRequest)HttpWebRequest.Create("http://jm/php/upload.php");
req.Method = WebRequestMethods.Http.Post;
req.ContentType = "multipart/form-data; boundary=" + boundary;
using (var reqStream = req.GetRequestStream()) {
var reqWriter = new StreamWriter(reqStream);
var tmp = string.Format(propFormat, "str1", "hello world");
reqWriter.Write(tmp);
tmp = string.Format(propFormat, "str2", "hello world 2");
reqWriter.Write(tmp);
reqWriter.Write("--" + boundary + "--");
reqWriter.Flush();
}
var res = req.GetResponse();
using (var resStream = res.GetResponseStream()) {
var reader = new StreamReader(resStream);
txt1.Text = reader.ReadToEnd();
}
Notice boundaries have to begin with -- {boundary declared in ContentType} and ending boundary must begin & end with -- . in my case, I originally used
var propFormat = boundary + newLine +
"Content-Disposition: form-data; name=\"{0}\"" + newLine + newLine +
"{1}" + newLine;
replace it with
var propFormat = "--" + boundary + newLine +
"Content-Disposition: form-data; name=\"{0}\"" + newLine + newLine +
"{1}" + newLine;
and everything works
I believe that you are not building the request body correctly.
First, you need to include part boundary (random text) in content type header. For example,
Content-Type: multipart/form-data;
boundary=----WebKitFormBoundarySkAQdHysJKel8YBM
Now format of request body will be something like
------WebKitFormBoundarySkAQdHysJKel8YBM
Content-Disposition: form-data;name="key"
KeyValueGoesHere
------WebKitFormBoundarySkAQdHysJKel8YBM
Content-Disposition: form-data;name="param2"
ValueHere
------WebKitFormBoundarySkAQdHysJKel8YBM
Content-Disposition: form-data;name="fileUpload"; filename="y1.jpg"
Content-Type: image/jpeg
[image data goes here]
I will suggest you to use tool such as Fiddler to understand how these requests are built.
This is nothing like multipart/form-data
There's no boundaries between fields (needed even with one field).
Why are you base-64 encoding?
There's no indication of the content-type of the image.
Take a look at RFC 2388 for the actual format spec. It can also be useful to look at a Fiddler grab of a file upload from a web-page.

Categories

Resources