C# Uploading image 400 bad request - c#

I'm programmatically trying to change my account's avatar for a website (ask.fm). I emulated the same identical request the browser is sending, but everytime I call it it gives me "400 Bad-request".
What I noticed with some debugging is that the boundary if changed gives error, so it may be they are using some algorithms for it instead of a random number, else I don't get why my code is not working.
Raw request from browser:
POST http://upload5 .ask. fm/upload/api-avatar HTTP/1.1
Host: upload5 .ask. fm
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: */*
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://ask. fm/account/settings/profile
Content-Length: 305167
Content-Type: multipart/form-data; boundary=---------------------------22412596920869
Origin: http://ask. fm
DNT: 1
Connection: keep-alive
-----------------------------22412596920869
Content-Disposition: form-data; name="file"; filename="1.png"
Content-Type: image/png
<...image bytes...>
-----------------------------22412596920869
Content-Disposition: form-data; name="specs"
U2FsdGVkX18XLeEmxsI+tytSpHcAV/UrBO8wmsoYERnL59rHGwF5Yz5QOeVl3GHap3ufLDGLyWxU4cCt28kPaSPq/iOusnVqRiYyp1nDD8VSvSLWa+Ndg8/TjKgaMqCgDNOrOSOPmxcV2kNPdDXNNvnuLWljYhPlBxrMGR2UeoqLNpJwsZCvs1UvVWcZFDy9SizVAOBDl6f1AIyqdvWiwjYGvg7jZ5q6ykTZda1pYuk=
-----------------------------22412596920869
Content-Disposition: form-data; name="ts"
1472985276554
-----------------------------22412596920869--
Raw request from my application:
POST http://upload5 .ask. fm/upload/api-avatar HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: */*
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://ask. fm/account/settings/profile
Origin: http://ask. fm
DNT: 1
Content-Type: multipart/form-data; boundary=-----------------------------680184
Host: upload5 .ask. fm
Content-Length: 305133
-----------------------------680184
Content-Disposition: form-data; name="file"; filename="1.png"
Content-Type: image/png
<...image bytes...>
-----------------------------680184
Content-Disposition: form-data; name="specs"
U2FsdGVkX19t+38KPvBBtpXmUUu8QGk61dKTvb0hEGZE165ot7tahOd1VZ1+uXbeGqy/GFM2uJ9Q/MTkStYZ4gITWc3/davB3NKJVcJN7xQR5/pNPMspR7PgCU2UhgWNFQuVHPhp9fiokIaR+QyqAOtQdd0nd6oFAIsKRPIBDjooo1sKE4BaXDHdQibNSBEdeJRgv1DjwtX77wEtXoV8DvS3+Z0sH2FJeY+iAY2bB2Q=
-----------------------------680184
Content-Disposition: form-data; name="ts"
1472985387451
-----------------------------680184--
So it's almost identical except the fact that there's no Keep-alive, I don't know why considering it's written in my request.
However, this is my code:
string Html = GetHtml("http://ask. fm/account/settings/profile");
string specs = Utils.GetSpecs(Html);
string ts = Utils.GetTs(Html);
Bitmap Image = new Bitmap(ImagePath);
MemoryStream mStream = new MemoryStream();
Image.Save(mStream, ImageFormat.Jpeg);
byte[] Bytes = mStream.ToArray();
string Boundary = "-----------------------------" + rnd.Next(100000, 900000);
string HeaderTemplate = #"Content-Disposition: form-data; name=""file""; filename=""1.png""\r\nContent-Type: ""image/png""\r\n\r\n";
HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("http://upload5 .ask.fm/upload/api-avatar");
postReq.AutomaticDecompression = DecompressionMethods.GZip;
WebHeaderCollection postHeaders = postReq.Headers;
postReq.Method = "POST";
postReq.Host = "upload5 .ask.fm";
postReq.ServicePoint.Expect100Continue = false;
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0";
postReq.Accept = "*/*";
postHeaders.Add("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3");
postHeaders.Add("Accept-Encoding", "gzip, deflate");
postReq.Referer = "http://ask .fm/account/settings/profile";
postHeaders.Add("Origin", "http://ask .fm");
postHeaders.Add("DNT", "1");
postReq.ContentType = "multipart/form-data; boundary=" + Boundary;
postReq.CookieContainer = Cookies;
postReq.KeepAlive = true;
string BoundaryHeader = Boundary + Environment.NewLine + #"Content-Disposition: form-data; name=""file""; filename=""{0}""" + Environment.NewLine + "Content-Type: image/png" + Environment.NewLine + Environment.NewLine;
string BoundaryEnd = Environment.NewLine + Boundary + Environment.NewLine + #"Content-Disposition: form-data; name=""specs""" + Environment.NewLine + Environment.NewLine + "{0}" + Environment.NewLine + Boundary + Environment.NewLine + #"Content-Disposition: form-data; name=""ts""" + Environment.NewLine + Environment.NewLine + "{1}" + Environment.NewLine + Boundary + "--";
byte[] BoundaryHeaderBytes = Encoding.UTF8.GetBytes(string.Format(BoundaryHeader, Path.GetFileName(ImagePath)));
byte[] BoundaryEndBytes = Encoding.ASCII.GetBytes(string.Format(BoundaryEnd, specs, ts));
FileStream fileStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
long length = BoundaryHeaderBytes.Length + fileStream.Length + BoundaryEndBytes.Length;
postReq.ContentLength = length;
Stream requestStream = postReq.GetRequestStream();
requestStream.Write(BoundaryHeaderBytes, 0, BoundaryHeaderBytes.Length);
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);
requestStream.Write(BoundaryEndBytes, 0, BoundaryEndBytes.Length);
WebResponse responce = postReq.GetResponse();
Stream s = responce.GetResponseStream();
StreamReader sr = new StreamReader(s);
What could the problem be? It's been some day since I'm struggling with this and I didn't find a solution yet, thank you in advance for any answers!

Related

C# HttpWebRequest send xhr request - 400 bad request

I have to send ajax request from C#. In browser the request looks like:
Request URL:https://sts-service.mycompany.com/UPNFromUserName
Request method:POST
Remote address:xxxx
Status code:
200
Version:HTTP/2.0
Referrer Policy:strict-origin-when-cross-origin
Headers:
Host: sts-service.mycompany.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://sts.mycompany.com/
Content-type: application/x-www-form-urlencoded
Content-Length: 17
Origin: https://sts.mycompany.com
DNT: 1
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Params:
Form data:
MS.Aution
Cookies: no cookie
And in C# my request:
WebRequest webRequest = WebRequest.Create("https://sts-service.mycompany.com/UPNFromUserName");
((HttpWebRequest)webRequest).Referer = "https://sts.mycompany.com/";
((HttpWebRequest)webRequest).Host = "sts-service.mycompany.com";
((HttpWebRequest)webRequest).KeepAlive = true;
((HttpWebRequest)webRequest).AllowAutoRedirect = true;
((HttpWebRequest)webRequest).UseDefaultCredentials = true;
((HttpWebRequest)webRequest).UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0";
webRequest.ContentType = "application/x-www-form-urlencoded";
((HttpWebRequest)webRequest).Accept = "*/*";
((HttpWebRequest)webRequest).Headers.Add("Origin", "https://sts.mycompany.com");
((HttpWebRequest)webRequest).Headers.Add("Accept-Encoding", "gzip, deflate, br");
((HttpWebRequest)webRequest).Headers.Add("Accept-Language", "en-US,en;q=0.5");
((HttpWebRequest)webRequest).Headers.Add("Upgrade-Insecure-Requests", #"1");
((HttpWebRequest)webRequest).Headers.Add("DNT", #"1");
((HttpWebRequest)webRequest).AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
webRequest.Method = HttpRequestType.POST.ToString();
string msg = "MSCnE.Automation";
webRequest.ContentLength = msg.Length;
Stream reqStream = webRequest.GetRequestStream();
byte[] msgb = System.Text.Encoding.UTF8.GetBytes(msg);
reqStream.Write(msgb, 0, msgb.Length);
reqStream.Close();
var response = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string Result = sr.ReadToEnd();
response.Close();
I get error:
The remote server returned an error: (400) Bad Request.
In browser in Network tab the request looks like:
Type is json but in Headers Content Type is application/x-www-form-urlencoded
Maybe this is the reason?
something along these lines (not tested):
public async Task<string> SendPOST()
{
var dict = new Dictionary<string, string>();
dict.Add("DNT", "1");
dict.Add("someformdata","MSCnE.Automation");
using (var formdata = new System.Net.Http.FormUrlEncodedContent(dict))
{
//do not use using HttpClient() - https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient
using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())
{
formdata.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
formdata.Headers.ContentType.CharSet = "UTF-8";
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0");
using (var response = await httpClient.PostAsync("https://sts-service.mycompany.com/UPNFromUserName", formdata))
{
if (response.IsSuccessStatusCode)
{
var postresult = await response.Content.ReadAsStringAsync();
return postresult;
}
else
{
string errorresult = await response.Content.ReadAsStringAsync();
return errorresult;
}
}
}
}
}

Unable to upload file using HTTP POST

I'm trying to upload a file using HTTP Post but somehow there is no file to be found when I process the request on the server side. I was able to create a similar request and successfully upload file using Chrome's Postman extension, but somehow can't do the same programmatically.
Client code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl);
request.Method = "POST";
using (Stream requestStream = request.GetRequestStream())
{
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
string header = string.Format(headerTemplate, "Files", "myFile.xml", "text/xml");
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
requestStream.Write(boundarybytes, 0, boundarybytes.Length);
requestStream.Write(headerbytes, 0, headerbytes.Length);
requestStream.Write(uploadedFile, 0, uploadedFile.Length);
requestStream.Write(trailer, 0, trailer.Length);
}
The request looks like this (in Fiddler) :
POST https://host/myUrl
Content-Length: 1067
Expect: 100-continue
Connection: Keep-Alive
------------8d2942f79ab208e
Content-Disposition: form-data; name="Files"; filename="myFile.xml"
Content-Type:text/xml
<myFile>
Something
</myFile>
------------8d2942f79ab208e
Server side:
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count != 1)
return BadRequest("Didn't get the file.");
But I always get httpRequest.Files.Count to be zero. Why?
The following request (created using Postman) gives me httpRequest.Files.Count to be one, as expected.
POST myUrl HTTP/1.1
Host: host
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="Files"; filename="myFile.xml"
Content-Type: text/xml
----WebKitFormBoundaryE19zNvXGzXaLvS5C
What am I doing wrong?
Figured it out. Thanks to this blog
Made two changes:
1) Added ContentType :
request.ContentType = "multipart/form-data; boundary=" + boundary;
2) Modified how the boundary ends
byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--");
And it works now. Hope this helps someone.
Perhaps you need to set the request's content type to "multipart/form-data"
request.ContentType = "multipart/form-data";

GET request with sockets returns corrupt data

I have a problem with sockets. I make GET request with this code:
using (s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
s.Connect(host, 80);
string requestS =
"GET http://" + adress + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
"Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3\r\n" +
"Accept-Encoding: gzip, deflate\r\n" +
"Connection: close\r\n\r\n";
Byte[] bytesSent = Encoding.UTF8.GetBytes(requestS);
Byte[] bytesReceived = new Byte[1000];
s.Send(bytesSent, bytesSent.Length, 0);
int bytes = 0;
do
{
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page2 += Encoding.UTF8.GetString(bytesReceived, 0, bytes);
m.Write(bytesReceived, 0, bytes);
}
while (bytes > 0);
}
And here is what i get from remote server:
HTTP/1.1 301 Moved Permanently
Date: Sat, 07 Mar 2015 10:02:48 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
X-Pingback: http://www.example.com/blog/xmlrpc.php
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Location: http://www.example.comhttp/www.example.com/somescript.php
Content-Length: 3
Connection: close
Content-Type: text/html; charset=UTF-8
As you see, location data is corrupted and I don't know how to fix this issue.
I don't see anything here that would cause this. The broken UTF-8 handling (as remarked in the comments) does not come into play for this bug because all chars here are ASCII.
The problem is somewhere else. Use Fiddler to make sure that the Location header is not being sent invalidly. Or, post executable repro code. Right now this is all that can be diagnosed.

PUT request to https site keeps returning status: 400 Bad request

I'm trying to make a PUT request to plug.dj with a httpWebRequest in c#, the problem i'm having is that i keep getting error 400(bad Request) but the request i made looks the same as the original one, am i misisng something?
These are the request headers that i got from chrome when i go to the network tab:
PUT /_/booth/lock HTTP/1.1
Host: plug.dj
Connection: keep-alive
Content-Length: 39
Accept: application/json, text/javascript, */*; q=0.01
Origin: https://plug.dj
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Content-Type: application/json
Referer: https://plug.dj/ao3
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: {Left out becouse it's used to authenticate}
The request payload and the request Url:
{"isLocked":true,"removeAllDJs":false}
https://plug.dj/_/booth/lock
And this is the one i created in c# using httpWebRequest(the authentication is correct so that's not the error):
var httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "PUT";
httpWebRequest.Accept = "application/json, text/javascript, */*; q=0.01";
httpWebRequest.Headers["Accept-Encoding"] = "gzip, deflate, sdch";
httpWebRequest.Headers["Accept-Language"] = "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4";
httpWebRequest.ContentLength = data.Length;
httpWebRequest.Headers["Cookie"] = GetCookies();
httpWebRequest.Host = "plug.dj";
httpWebRequest.Headers["Origin"] = "https://plug.dj";
httpWebRequest.Referer = "https://plug.dj/" + _room;
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36";
httpWebRequest.Headers["X-Requested-With"] = "XMLHttpRequest";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(data);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
return streamReader.ReadToEnd();
}

how to upload file via c# post request? ownCloud

i`m using ownCloud (open source cloud) and i have a form to upload files
the form sending the post request to upload.php file that handle the upload.
the request have a lot of fields and need to send all info and cookie.
i need to develop a c# code to upload files to the cloud.
the best way in my opinion is to make a request similar to the request that the form does.
what do you think? any suggestions?
p.s i read the following solutions but it is not working.
Sending Files using HTTP POST in c#
http://bytes.com/topic/c-sharp/answers/268661-how-upload-file-via-c-code
Upload files with HTTPWebrequest (multipart/form-data)
thanks
here is some of the code:
the form:
<form data-upload-id='1'
id="data-upload-form"
class="file_upload_form"
action="<?php print_unescaped(OCP\Util::linkTo('files', 'ajax/upload.php')); ?>"
method="post"
enctype="multipart/form-data"
target="file_upload_target_1">
<input type="hidden" name="MAX_FILE_SIZE" id="max_upload" value="<?php p($_['uploadMaxFilesize']) ?>">
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" id="requesttoken">
<input type="hidden" class="max_human_file_size" value="(max <?php p($_['uploadMaxHumanFilesize']); ?>)">
<input type="hidden" name="dir" value="<?php p($_['dir']) ?>" id="dir">
<input type="file" id="file_upload_start" name='files[]'/>
</form>
This the way the request seen to me:
enter code here
Request URL:http://my-url/owncloud/index.php/apps/files/ajax/upload.php
Request Method:POST
Status Code:200 OK
Request Headersview parsed
POST /owncloud/index.php/apps/files/ajax/upload.php HTTP/1.1
Host: my-url
Connection: keep-alive
Content-Length: 730
Accept: */*
requesttoken: 0bbcd458174e76e139ad
Origin: http://my-url
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Referer: http://my-url/owncloud/index.php/apps/files
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: oc_username=tal; oc_token=f24c041e992624d10cabbaa16aa6aeea; oc_remember_login=1; __utma=220528984.2016256779.1375771228.1375771228.1375862096.2; __utmz=220528984.1375771228.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); olfsk=olfsk4827894743066281; hblid=QHgh67nWTyXfpzWe6B1Tj9Z3JM0QBCfA; 510e8a1de6274=eqavm1nikkon6ush7con3o6ar6
Request Payload
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Content-Disposition: form-data; name="MAX_FILE_SIZE"
537919488
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Content-Disposition: form-data; name="requesttoken"
0bbcd458174e76e139ad
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Content-Disposition: form-data; name="dir"
/
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Content-Disposition: form-data; name="files[]"; filename="bg.png"
Content-Type: image/png
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT--
Response Headersview parsed
HTTP/1.1 200 OK
Date: Sun, 08 Sep 2013 07:21:02 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze16
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Content-Type-Options: nosniff
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 132
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: text/plain; charset=utf-8
i tried this code
public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream rs = wr.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
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\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, paramName, file, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, 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("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
//StreamReader reader3 = new StreamReader(rs);
rs.Close();
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
MessageBox.Show((string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd())));
Debug.WriteLine(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
}
catch(Exception ex)
{
Debug.WriteLine("Error uploading file", ex);
if(wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
}
the calling function:
NameValueCollection nvc = new NameValueCollection();
nvc.Add("id", "TTR");
nvc.Add("btn-submit-photo", "Upload");
FilesClass.HttpUploadFile("http://192.168.49.108/owncloud/index.php/apps/files/ajax/upload.php", #"C:\t.txt", "files[]", "text/plain", nvc);
the response from the server is {"data":{"message":"Authentication error"},"status":"error"}
that mean that i was rejected by upload.php
maybe i need to send the cookie?
Instead of trying to simulate a POST upload (which ownCloud makes fairly difficult due to security issues) you can use WebDAV to upload the file.
Simply send a PUT request to http://example.com/owncloud/remote.php/webdav/some/path

Categories

Resources