Upload image to web API using C# - c#

I am trying to upload an image to a web service via POST.
The API documentation says "to upload files via POST, encoded as "multipart/form-data" and include a POST arg named "image" with the image data. Images must be of type PNG, JPG, or GIF."
This is my code:
Bitmap myImage = new Bitmap("myImage.jpg");
byte[] myFileData = (byte[])(new ImageConverter()).ConvertTo(myImage, typeof(byte[]));
string myBoundary = "------------------------" + DateTime.Now.Ticks;
var newLine = Environment.NewLine;
string myContent =
"--" + myBoundary + newLine +
"content-disposition: form-data; name=\"image\"; filename=\"myImage.jpg\"" + newLine +
"Content-Type: image/jpeg" + newLine +
"Content-Transfer-Encoding: binary" + newLine +
newLine +
Encoding.Default.GetString(myFileData) + newLine +
"--" + myBoundary + "--";
try {
using (var httpClient = new HttpClient())
using (var content = new StringContent(myContent, Encoding.Default, "multipart/form-data, boundary=" + myBoundary))
using (var response = await httpClient.PostAsync("http://my_API_URL", content)) {
string responseData = await response.Content.ReadAsStringAsync();
}
}
catch (Exception myExp) { }
This code raises an exception trying to create the StringContent object.
I am ok for any suggestions. The API that I need to use requires Authentication, which typically is solved using a WebClient and this statement:
client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("my_API_key:"));
I am OK with using any other form of POST, like WebClient or HttpClient.

Code that finally worked, in case anyone is looking for the same answer:
Bitmap myImage = new Bitmap("myImage.jpg");
byte[] myFileData = (byte[])(new ImageConverter()).ConvertTo(myImage, typeof(byte[]));
string myBoundary = "---------------------------7df3c13714f0ffc";
var newLine = Environment.NewLine;
string myContent =
"--" + myBoundary + newLine +
"Content-Disposition: form-data; name=\"image\"; filename=\"myImage.jpg\"" + newLine +
"Content-Type: image/jpeg" + newLine +
newLine +
Encoding.Default.GetString(myFileData) + newLine +
"--" + myBoundary + "--";
using (var client = new WebClient()) {
try {
client.Headers["Authorization"] = "Basic xxxxxx";
client.Headers["Content-Type"] = "multipart/form-data; boundary=" + myBoundary;
client.UploadString(new Uri(myURL), "POST", myContent);
totalAPICalls++;
}
catch { }
}

Related

Azure Storage Queue via REST API c# using Shared Key Authentication

I am trying to call Azure Storage queue using REST API, but I am getting an error
The MAC signature found in the HTTP request
'UCiypkoySXueF4scXt+EqQESf5VXmAVLJUA93+3W10M=' is not the same as any
computed signature. The server used following string to sign: 'POST
text/plain
My C# Code is
var Client = new HttpClient();
var RequestDateString = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
if (Client.DefaultRequestHeaders.Contains("x-ms-date"))
Client.DefaultRequestHeaders.Remove("x-ms-date");
Client.DefaultRequestHeaders.Add("x-ms-date", RequestDateString);
var StorageAccountName = "storaxxxxxxxsnd";
var StorageKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==";
String urlPath = String.Format("{0}/messages", "splitator");
Uri uri = new Uri(string.Format("https://{0}.queue.core.windows.net/", StorageAccountName) + urlPath);
if (Client.DefaultRequestHeaders.Contains("Authorization"))
Client.DefaultRequestHeaders.Remove("Authorization");
var canonicalizedStringToBuild = string.Format("{0}\n{1}", RequestDateString, $"/{StorageAccountName}/{uri.AbsolutePath.TrimStart('/')}");
string signature;
using (var hmac = new HMACSHA256(Convert.FromBase64String(StorageKey)))
{
byte[] dataToHmac = Encoding.UTF8.GetBytes(canonicalizedStringToBuild);
signature = Convert.ToBase64String(hmac.ComputeHash(dataToHmac));
}
string authorizationHeader = string.Format($"{StorageAccountName}:" + signature);
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SharedKey", authorizationHeader);
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
if (Client.DefaultRequestHeaders.Contains("x-ms-version"))
Client.DefaultRequestHeaders.Remove("x-ms-version");
Client.DefaultRequestHeaders.Add("x-ms-version", "2015-12-11");
// if (httpMethod == HttpMethod.Delete || httpMethod == HttpMethod.Put)
// {
// if (Client.DefaultRequestHeaders.Contains("If-Match"))
// Client.DefaultRequestHeaders.Remove("If-Match");
// Currently I'm not using optimistic concurrency :-(
try
{
//Client.DefaultRequestHeaders.Add("If-Match", "*");
var stringContent = new StringContent("TESTAUTH", Encoding.UTF8, "text/plain");
var response= Client.PostAsync(uri, stringContent);
var resu=response.Result;
}
catch(Exception ex)
{
}
I am not sure what I am missing. I tried various combination but its failing.
I tried Microsoft recommended stringToSign formula too
I tried using canonical headers too
string signature;
var stringTosign = "POST\n" + "\n" + "\n" + "1024" + "\n" + "\n" + "text/plain\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + dateInRfc1123Format + "/xxxxxx/splitator/messages";
var hmac = new HMACSHA256(Convert.FromBase64String(accountKey));
var headerval= accountName + ":" + Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringTosign)));
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SharedKey", headerval);
Client.DefaultRequestHeaders.Accept.Clear();
I fixed the issue in your code, and now it's working. Please give it a try:
namespace ConsoleApp25
{
class Program
{
static void Main(string[] args)
{
var Client = new HttpClient();
var StorageAccountName = "yy1";
var StorageKey = "xxxx";
var apiversion = "2020-02-10";
var queue_name = "myqueue2";
String urlPath = String.Format("{0}/messages", queue_name);
Uri uri = new Uri(string.Format("https://{0}.queue.core.windows.net/{1}", StorageAccountName, urlPath));
//define a message to send
string raw_message = "TESTAUTH is ok";
//to send the message to the queue storage, the raw message must be formatted as below
string queue_message = $"<QueueMessage><MessageText>{raw_message}</MessageText></QueueMessage>";
//define the content type
string content_type = "text/plain; charset=utf-8";
//define date
var RequestDateString = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
string StringToSign = String.Format("POST\n"
+ "\n" // content encoding
+ "\n" // content language
+ queue_message.Length + "\n" // content length
+ "\n" // content md5
+ content_type +"\n" // content type
+ "\n" // date
+ "\n" // if modified since
+ "\n" // if match
+ "\n" // if none match
+ "\n" // if unmodified since
+ "\n" // range
+ "x-ms-date:" + RequestDateString + "\nx-ms-version:" + apiversion + "\n" // headers
+ "/{0}/{1}/{2}", StorageAccountName, queue_name, "messages"); //url
string auth = SignThis(StringToSign, StorageKey, StorageAccountName);
//define authorization header
if (Client.DefaultRequestHeaders.Contains("Authorization"))
Client.DefaultRequestHeaders.Remove("Authorization");
Client.DefaultRequestHeaders.Add("Authorization", auth);
Client.DefaultRequestHeaders.Accept.Clear();
//define x-ms-version header
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
if (Client.DefaultRequestHeaders.Contains("x-ms-version"))
Client.DefaultRequestHeaders.Remove("x-ms-version");
Client.DefaultRequestHeaders.Add("x-ms-version", apiversion);
//define the x-ms-date header
if (Client.DefaultRequestHeaders.Contains("x-ms-date"))
Client.DefaultRequestHeaders.Remove("x-ms-date");
Client.DefaultRequestHeaders.Add("x-ms-date", RequestDateString);
try
{
var stringContent = new StringContent(queue_message, Encoding.UTF8, "text/plain");
var response = Client.PostAsync(uri, stringContent);
var resu = response.Result;
}
catch (Exception ex)
{
}
Console.WriteLine("**completed**");
Console.ReadLine();
}
private static String SignThis(String StringToSign, string Key, string Account)
{
String signature = string.Empty;
byte[] unicodeKey = Convert.FromBase64String(Key);
using (HMACSHA256 hmacSha256 = new HMACSHA256(unicodeKey))
{
Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(StringToSign);
signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
}
String authorizationHeader = String.Format(
CultureInfo.InvariantCulture,
"{0} {1}:{2}",
"SharedKey",
Account,
signature);
return authorizationHeader;
}
}
}
And if you don't want to generate the shared key since it's not easy, you can use sas token for authentication in the rest api.

Converting WebClient to HttpClient

I have been surfing and working on this for hours now and I'm read to throw my PC.
I have a WebClient on WPF application that needs to be converted to HttpClient for UWP application. I am getting a lot of header errors and since I'm not a web guru I am beating myself.
Here's the original and if someone would be extremely kind and help me convert it to HttpClient I would be very thankful.
using (var client = new WebClient())
{
string request = "------WebKitFormBoundarygWsJMIUcbjwBPfeL"
+ Environment.NewLine
+ "Content-Disposition: form-data; name=\"guid\""
+ Environment.NewLine
+ Environment.NewLine
+ presetSmall.PresetId.ToString()
+ Environment.NewLine
+ "------WebKitFormBoundarygWsJMIUcbjwBPfeL"
+ Environment.NewLine
+ "Content-Disposition: form-data; name=\"delay\""
+ Environment.NewLine
+ Environment.NewLine
+ "0"
+ Environment.NewLine
+ "------WebKitFormBoundarygWsJMIUcbjwBPfeL--"
+ Environment.NewLine;
client.Headers.Add(HttpRequestHeader.ContentType, "multipart/form-data; boundary=----WebKitFormBoundarygWsJMIUcbjwBPfeL");
client.UploadStringAsync(new Uri(ServerName + UriForPresetExecution), "POST", request);
}
The problem is in the client.Headers.Add method... I cannot for the sake of time figure out how HttpClient wants me to add those headers.
Please try this:
using System.Net.Http;
using System.Net.Http.Headers;
using (var httpClient = new HttpClient())
{
var httpContent = new StringContent(request);
httpContent.Headers.Clear();
httpContent.Headers.Add("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarygWsJMIUcbjwBPfeL");
var response = await httpClient.PostAsync(requestUri, httpContent);
}

Twitter OAuth request_token stopped working

Out of no where, my twitter API calls, specificly my first step in the 3-legged auth, stopped working. I've compared the timestamps, keys and everything with the OAuth signature generator tool, and they all match (execpt oauth_nonce but thats the point I guess). Here is my code. Any suggestions or small observations would be appreciated.
protected void RequestToken()
{
string oauthcallback = Request.Url.Host + "/TwitterCallback.aspx";
string oauthconsumerkey = "xxx-consumerkey";
string oauthconsumersecret = "xxx-consumerSecret";
string oauthtokensecret = string.Empty;
string oauthtoken = string.Empty;
string oauthsignaturemethod = "HMAC-SHA1";
string oauthversion = "1.0";
string oauthnonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
string oauthtimestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
string url = "https://api.twitter.com/oauth/request_token?oauth_callback=" + oauthcallback;
SortedDictionary<string, string> basestringParameters = new SortedDictionary<string, string>();
basestringParameters.Add("oauth_version", oauthversion);
basestringParameters.Add("oauth_consumer_key", oauthconsumerkey);
basestringParameters.Add("oauth_nonce", oauthnonce);
basestringParameters.Add("oauth_signature_method", oauthsignaturemethod);
basestringParameters.Add("oauth_timestamp", oauthtimestamp);
basestringParameters.Add("oauth_callback", Uri.EscapeDataString(oauthcallback));
//Build the signature string
string baseString = String.Empty;
baseString += "POST" + "&";
baseString += Uri.EscapeDataString(url.Split('?')[0]) + "&";
foreach (KeyValuePair<string, string> entry in basestringParameters)
{
baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&");
}
//Remove the trailing ambersand char last 3 chars - %26
//baseString = baseString.Substring(0, baseString.Length - 3);
//Build the signing key
string signingKey = Uri.EscapeDataString(oauthconsumersecret) +
"&" + Uri.EscapeDataString(oauthtokensecret);
//Sign the request
HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
string oauthsignature = Convert.ToBase64String(
hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));
//Tell Twitter we don't do the 100 continue thing
ServicePointManager.Expect100Continue = false;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(#url);
string authorizationHeaderParams = String.Empty;
authorizationHeaderParams += "OAuth ";
authorizationHeaderParams += "oauth_nonce=" + "\"" +
Uri.EscapeDataString(oauthnonce) + "\",";
authorizationHeaderParams += "oauth_signature_method=" + "\"" +
Uri.EscapeDataString(oauthsignaturemethod) + "\",";
authorizationHeaderParams += "oauth_timestamp=" + "\"" +
Uri.EscapeDataString(oauthtimestamp) + "\",";
authorizationHeaderParams += "oauth_consumer_key=" + "\"" +
Uri.EscapeDataString(oauthconsumerkey) + "\",";
authorizationHeaderParams += "oauth_signature=" + "\"" +
Uri.EscapeDataString(oauthsignature) + "\",";
authorizationHeaderParams += "oauth_version=" + "\"" +
Uri.EscapeDataString(oauthversion) + "\"";
webRequest.Headers.Add("Authorization", authorizationHeaderParams);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
//Allow us a reasonable timeout in case Twitter's busy
webRequest.Timeout = 3 * 60 * 1000;
try
{
HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
Stream dataStream = webResponse.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
var uri = new Uri("https://test.dk?" + responseFromServer);
var token = HttpUtility.ParseQueryString(uri.Query).Get("oauth_token"); ;
var tokensecret = HttpUtility.ParseQueryString(uri.Query).Get("oauth_token_secret");
Response.Write(responseFromServer);
Response.Redirect("https://api.twitter.com/oauth/authorize?force_login=true&oauth_token=" + token);
}
catch (Exception ex)
{
Response.Write(ex.GetBaseException());
}
}
The error obviously happens when I do the HTTP request webRequest.GetResponse()
It returns a 401 unauthorized
Apperently you have to include the oauth version number in the URL now, or else it will fall back to the oldest version (or maybe the newest, can't remember).
Providing /oath/1.0/ or /1.0/oauth/ or what ever solved my issue as i recall it (it's been a while).

Exchange WebDAV Save to Drafts NOT editable

I am having the following code to Save message to Exchange WEBDAV Drafts folder. IT saves the message but If open it in Outlook, Send button is Disabled and the message is readonly. Please help me find what am i missing in this code...
thanks
Bhuvan
strBody = "To: " + strTo + "\n" +
"Subject: " + (string.IsNullOrEmpty(message.Subject) ? "" : message.Subject) + "\n" +
"Date: " + System.DateTime.Now + "\n" +
"X-Mailer: test mailer" + "\n" +
"MIME-Version: 1.0" + "\n" +
"Content-Type: text/html;" + "\n" +
"Charset = \"iso-8859-1\"" + "\n" +
"Content-Transfer-Encoding: 8bit" + "\n" +
"\n" + (string.IsNullOrEmpty(message.HtmlBody) ? "" : message.HtmlBody.Replace("<head />","").Replace("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",""));
// Create the HttpWebRequest object.
PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(tgtUri);
// Add the network credentials to the request.
// Use default credentials of the service to access the server.
PUTRequest.Credentials = cred;
// Specify the PUT method.
PUTRequest.Method = "PUT"; //PROPPATCH
// Encode the body using UTF-8.
byte[] bytes = Encoding.UTF8.GetBytes((string)strBody);
// Set the content header length. This must be
// done before writing data to the request stream.
PUTRequest.ContentLength = bytes.Length;
// Get a reference to the request stream.
PUTRequestStream = PUTRequest.GetRequestStream();
// Write the message body to the request stream.
PUTRequestStream.Write(bytes, 0, bytes.Length);
// Close the Stream object to release the connection
// for further use.
PUTRequestStream.Close();
// Set the Content-Type header to the RFC 822 message format.
PUTRequest.ContentType = "message/rfc822";
// PUT the message in the Drafts folder of the
// sender's mailbox.
PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse();
if (message.Attachments != null)
{
//Do the PROPPATCH
System.Net.HttpWebRequest PROPPATCHRequest;
System.Net.WebResponse PROPPATCHResponse;
System.IO.Stream PROPPATCHRequestStream = null;
string strxml = "<?xml version='1.0'?>" +
"<d:propertyupdate xmlns:d='DAV:'>" +
"<d:set>" +
"<d:prop>" +
"<isCollection xmlns='DAV:'>False</isCollection>" +
"</d:prop>" +
"</d:set>"
+ "<g:remove>"
+ "<g:prop><m:date/></g:prop>"
+ "</g:remove>"
+ "</d:propertyupdate>";
PROPPATCHRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(tgtUri);
PROPPATCHRequest.Credentials = cred;
PROPPATCHRequest.Headers.Set("Translate", "f");
PROPPATCHRequest.ContentType = "text/xml";
PROPPATCHRequest.ContentLength = strxml.Length;
PROPPATCHRequest.Method = "PROPPATCH";
byte[] PROPPATCHbytes = Encoding.UTF8.GetBytes(strxml);
PROPPATCHRequest.ContentLength = PROPPATCHbytes.Length;
PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream();
PROPPATCHRequestStream.Write(PROPPATCHbytes, 0, PROPPATCHbytes.Length);
PROPPATCHRequestStream.Close();
PROPPATCHResponse = (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse();
foreach (KeyValuePair<string, byte[]> attachment in message.Attachments)
{
System.IO.Stream PUTAttachRequestStream = null;
System.Net.HttpWebRequest PUTAttachRequest;
System.Net.WebResponse PUTAttachResponse;
//Attach File: This could be put in a loop to attach more than one file.
string FileName = attachment.Key; //#"2842498_794802035296_Label1.pdf";
string attachURI = tgtUri + "/" + FileName;
PUTAttachRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(attachURI);
PUTAttachRequest.Credentials = cred;
PUTAttachRequest.Method = "PUT";
PUTAttachRequest.ContentLength = attachment.Value.Length; //binaryData.Length;
PUTAttachRequestStream = PUTAttachRequest.GetRequestStream();
PUTAttachRequestStream.Write(attachment.Value, 0, attachment.Value.Length); //(binaryData, 0, binaryData.Length);
PUTAttachRequestStream.Close();
PUTAttachResponse = (System.Net.HttpWebResponse)PUTAttachRequest.GetResponse();
PUTAttachResponse.Close();
}
PROPPATCHResponse.Close();
}
// Clean up.
PUTResponse.Close();
I found the answer in Akash's blog on msdn. One has to carefully read this to get the actual answer, but the solution worked.

c# multipart/form-data submit programmatically

So got an small problem. Im creating an small application to automate an form submission on one website. But the bad thing is that they are using multipart/form-data for that.
There is no file uploading just some text fields for submission.
Of course doing it like this it fails.
string postData1 = "firstfield="+firststring+"secondfield="+secondstring;
So my question is how the hell post those form fields with multipart form?
Posting like arrays in php like this:
$postdata = array('firstfield' => $firststring, 'secondfield' => $secondstring);
works and passes the form but seems not working with c#
Any suggestions?
Data submission goes through 3 page ( basic screenscrape ) login/part1/part2
So far i can log in successfully and post part1 (uses normal application/x-www-form-urlencoded form )
But when ill try to post multipart form it fails and sends me back to part1. So maybe is my code wrong but here it is:
string password = "password";
string username = "username";
string link = "http://somelink.com/";
string text = "Blah Blah some text here";
string title = "Blah Blah";
string tags1 = title;
string summary = "Blah Blah summary";
string tags = tags1.Replace(" ", ",");
// Set cookie container
CookieContainer cookieJar = new CookieContainer();
string loginData = "username=" + username + "&password=" + password + "&processlogin=1&return=%2Fsubmit.php";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://loginlink.com/login.php");
myRequest.Method = "POST";
myRequest.ServicePoint.Expect100Continue = false;
myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest.Timeout = 10000;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = loginData.Length;
myRequest.CookieContainer = cookieJar;
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter = new StreamWriter(myRequest.GetRequestStream());
myWriter.Write(loginData);
myWriter.Close();
// Get the response.
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader = new StreamReader(myResponse.GetResponseStream());
// Read the content.
string output = myReader.ReadToEnd();
// Clean up the streams and the response.
myReader.Close();
myResponse.Close();
Match matchkey = Regex.Match(output, "type=\"hidden\" name=\"randkey\" value=\"([^\"]+)\"", RegexOptions.IgnoreCase);
string key1 = matchkey.Groups[1].Value;
Match matchid = Regex.Match(output, "type=\"hidden\" name=\"id\" value=\"([^\"]+)\"", RegexOptions.IgnoreCase);
string id1 = matchid.Groups[1].Value;
string postData = "url=" + link + "&phase=1&randkey=" + key1 + "&id=" + id1;
HttpWebRequest myRequest2 = (HttpWebRequest)WebRequest.Create("http://submitpage1.com/submit.php");
myRequest2.Method = "POST";
myRequest2.ServicePoint.Expect100Continue = false;
myRequest2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest2.Timeout = 10000;
myRequest2.ContentType = "application/x-www-form-urlencoded";
myRequest2.ContentLength = postData.Length;
myRequest2.CookieContainer = cookieJar;
myRequest2.KeepAlive = true;
myRequest2.AllowAutoRedirect = true;
//Write post data to stream
StreamWriter myWriter2 = new StreamWriter(myRequest2.GetRequestStream());
myWriter2.Write(postData);
myWriter2.Close();
// Get the response.
HttpWebResponse myResponse2 = (HttpWebResponse)myRequest2.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader2 = new StreamReader(myResponse2.GetResponseStream());
// Read the content.
string output1 = myReader2.ReadToEnd();
// Clean up the streams and the response.
myReader2.Close();
myResponse2.Close();
Match matchkey1 = Regex.Match(output1, "type=\"hidden\" name=\"randkey\" value=\"([^\"]+)\"", RegexOptions.IgnoreCase);
string key2 = matchkey1.Groups[1].Value;
Match matchid1 = Regex.Match(output1, "type=\"hidden\" name=\"randkey\" value=\"([^\"]+)\"", RegexOptions.IgnoreCase);
string id2 = matchid1.Groups[1].Value;
string boundary = "-----------------------------1721856231228";
// Build up the post
StringBuilder sb = new StringBuilder();
sb.Append("\r\n" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"title\"" + "\r\n");
sb.Append("\r\n");
sb.Append(title);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"tags\"" + "\r\n");
sb.Append("\r\n");
sb.Append(tags);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"bodytext\"" + "\r\n");
sb.Append("\r\n");
sb.Append(text);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"summarycheckbox\"" + "\r\n");
sb.Append("\r\n");
sb.Append("on");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"summarytext\"" + "\r\n");
sb.Append("\r\n");
sb.Append(summary);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"remLen\"" + "\r\n");
sb.Append("\r\n");
sb.Append("125");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"category\"" + "\r\n");
sb.Append("\r\n");
sb.Append("1");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"trackback\"" + "\r\n");
sb.Append("\r\n");
sb.Append("");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"url\"" + "\r\n");
sb.Append("\r\n");
sb.Append(link);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"phase\"" + "\r\n");
sb.Append("\r\n");
sb.Append("2");
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"randkey\"" + "\r\n");
sb.Append("\r\n");
sb.Append(key2);
sb.Append("\r\n--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"id\"" + "\r\n");
sb.Append("\r\n");
sb.Append(id2);
sb.Append("\r\n--" + boundary + "--" + "\r\n");
string postData1 = sb.ToString();
HttpWebRequest myRequest3 = (HttpWebRequest)WebRequest.Create("http://submitpage2.com/submit.php");
myRequest3.Method = "POST";
myRequest3.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 GTB6 (.NET CLR 3.5.30729)";
myRequest3.Timeout = 10000;
myRequest3.ServicePoint.Expect100Continue = false;
myRequest3.Referer = "http://bookmarkindo.com/submit.php";
myRequest3.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myRequest3.ContentType = "multipart/form-data; boundary=" + boundary;
myRequest3.ContentLength = postData1.Length;
myRequest3.CookieContainer = cookieJar;
myRequest3.KeepAlive = true;
myRequest3.AllowAutoRedirect = true;
//Write out postdata
StreamWriter myWriter3 = new StreamWriter(myRequest3.GetRequestStream());
myWriter3.Write(postData1);
myWriter3.Close();
// Get the response.
HttpWebResponse myResponse3 = (HttpWebResponse)myRequest3.GetResponse();
// Open the stream using a StreamReader for easy access.
StreamReader myReader3 = new StreamReader(myResponse3.GetResponseStream());
// Read the content.
string output2 = myReader3.ReadToEnd();
// Clean up the streams and the response.
myReader3.Close();
myResponse3.Close();
All suggestions are welcome
posts os multipart/form-data type have a different structure because they are meant to transfer data and not just plain text.
Here's the format:
--[random number, a GUID is good here]
Content-Disposition: form-data; name="[name of variable]"
[actual value]
--[random number, a GUID is good here]--
Using HTTPWebRequest you can create a request that has that format. Here's a sample:
string boundary = Guid.NewGuid().ToString();
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "username"));
contents.AppendLine();
contents.AppendLine("your_username");
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "password"));
contents.AppendLine();
contents.AppendLine("your_password");
contents.AppendLine(footer);
The best way to send multipart form data in C# is shown in the snippet, here you see that adding different type of content is as easy as adding it to the wrapper multipart content type:
var documentContent = new MultipartFormDataContent();
documentContent.Add(new StringContent("AnalyticsPage.xlsx"), "title");
documentContent.Add(new ByteArrayContent(File.ReadAllBytes("C:\\Users\\awasthi\\Downloads\\AnalyticsPage.xlsx")), "file", "AnalyticsPage.xlsx");
Then just make an api call:
using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, CookieContainer = new CookieContainer() }))
{
response = client.PostAsync(documentAddApi, documentContent).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
}
Here the expectation is that the rest endpoint you are making a call to is accepting a 'title' field for the file and the byte array of the file named 'file'.
Here is an article on multipart form posts in C# with more detail. This code was eventually merged into RestSharp, which is an excellent library you could use to generate the request.
The format of multipart/form-data requests is outlined here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2.

Categories