I'm trying to create a Bulk API to send Data to Salesforce .Here is my Code`
string userName = "XXXX";
//Console.Write("Enter password: ");
string password = "XXX";
string SessionId = SalesforceLogin(userName, password);
string JobId = CreateJob(SessionId, "insert", "Contact");
byte[] inputFileData = null;
string jobId = string.Empty;
string resultId = string.Empty;
string batchId = string.Empty;
if (JobId.Length > 0)
{
System.IO.FileInfo oFile = null;
//oFile = New System.IO.FileInfo("data.csv")
oFile = new System.IO.FileInfo(#"D:\request.txt");
System.IO.FileStream oFileStream = oFile.OpenRead();
long lBytes = oFileStream.Length;
int a = (int)lBytes; //modified after conversion
if ((lBytes > 0))
{
byte[] fileData = new byte[lBytes];
// Read the file into a byte array
oFileStream.Read(fileData, 0, a); //modified after conversion
oFileStream.Close();
//Get the file where the Query is present
inputFileData = fileData;
}
batchId= AddBatch(SessionId, JobId, inputFileData);
After that, I create A Job
private static string CreateJob(string sessionId, string sfOperation, string sfObjectName)
{
string str = "";
string reqURL = "";
byte[] bytes;
XmlDocument reqDoc;
XmlDocument respDoc;
str = ""
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" // added \r\n as recommended by L.B's answer
+ "<jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">"
+ " <operation></operation>" // removed "+sfOperation+"
+ " <object></object>" // removed "+sfObjectName+"
+ " <contentType>XML</contentType>" // should be CSV, NOT XML
+ "</jobInfo>"
;
reqURL = "https://ap2.salesforce.com/services/async/23.0/job";
reqDoc = new XmlDocument();
reqDoc.LoadXml(str);
// added XML modifications
reqDoc.GetElementsByTagName("operation")[0].InnerText = sfOperation;
reqDoc.GetElementsByTagName("object")[0].InnerText = sfObjectName;
bytes = System.Text.Encoding.ASCII.GetBytes(reqDoc.InnerXml);
respDoc = Post(bytes, reqURL, sessionId); // create job
string JobId = (respDoc != null) ?
(respDoc.GetElementsByTagName("id").Count > 0) ?
(respDoc.GetElementsByTagName("id")[0].InnerText) :
"" :
""
;
return JobId;
}`
Then I create A Batch to
private static string AddBatch(string sessionId, string jobId, byte[] fileBytes)
{
string reqURL = "https://ap2.salesforce.com/services/async/23.0/job/" + jobId + "/batch";
XmlDocument respDoc = Post(fileBytes, reqURL, sessionId);
string batchId = (respDoc != null) ?
(respDoc.GetElementsByTagName("id").Count > 0) ?
(respDoc.GetElementsByTagName("id")[0].InnerText) :
"" :
""
;
return batchId;
}
Then the Post Method
private static XmlDocument Post(byte[] bytes, string reqURL, object sfSessionId)
{
WebRequest req = WebRequest.Create(reqURL);
req.Method = "POST";
if ((bytes != null))
{
req.ContentLength = bytes.Length;
}
req.ContentType = "application/xml; charset=UTF-8"; // should be text/csv; when passing a CSV file
req.Headers.Add("X-SFDC-Session: " + sfSessionId);
System.IO.Stream strm = req.GetRequestStream();
if ((bytes != null))
strm.Write(bytes, 0, bytes.Length);
strm.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
System.IO.Stream respStrm = resp.GetResponseStream();
XmlDocument respDoc = new XmlDocument();
respDoc.Load(respStrm);
return respDoc;
}
Everything Going Good But when I'm trying to save the Data on Lead it displays the Error
InvalidBatch : Failed to parse XML. Failed to get next element
After Creating Batch I get Result
<?xml version="1.0" encoding="UTF-8"?><batchInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload"><id>7512800000C8OHAAA3</id><jobId>75028000008qk53AAA</jobId><state>Queued</state><createdDate>2017-05-30T14:11:45.000Z</createdDate><systemModstamp>2017-05-30T14:11:45.000Z</systemModstamp><numberRecordsProcessed>0</numberRecordsProcessed><numberRecordsFailed>0</numberRecordsFailed><totalProcessingTime>0</totalProcessingTime><apiActiveProcessingTime>0</apiActiveProcessingTime><apexProcessingTime>0</apexProcessingTime></batchInfo>
One More thing After inserting the Batch, How this job will execute .I'm pasting all the code because it may help Someone if i get the Answer :).
Related
I am trying to connect to ICloud calendars and parse events I can do that if I use the public calendar url and it works fine. However doing that I cannot update the event for that I need CalDav connection.
I am struggling as most of the examples here return 404 Not Found.
Can anyone please guide me "ICloud Caldav connection to get Calendars " in C# a sample or working example.
Keeping in mind the for the above I will only have Username and Password for the account so with that provided I have to load list of Calendar's.
Found this article very helpful in achieving the task:
Sample code:
static string calendarURI = "https://caldav.icloud.com/";
static string username = "abc#icloud.com";
static string password = "xxxx-xxxx-xxxx-xxxx";
//static string content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><propfind xlmns=\"DAV:\"><allprop/></propfind>";
static string content = "<propfind xmlns='DAV:'><prop><current-user-principal/></prop></propfind>";
public async static void GetCalendars()
{
try
{
ResponseStream = ExectueMethod(username, password, calendarURI, "PROPFIND", null, content, "application/x-www-form-urlencoded","0");
XmlDocument = new XmlDocument();
XmlDocument.Load(ResponseStream);
string xmlInner = XmlDocument.InnerXml;
XmlDocument innerXmlDocument = new XmlDocument();
innerXmlDocument.LoadXml(xmlInner);
XmlNode statusCheck = innerXmlDocument.GetElementsByTagName("status")[0];
if (statusCheck != null)
{
string status = statusCheck.InnerText.Trim();
if (status == "HTTP/1.1 200 OK")
{
XmlNode tag = innerXmlDocument.GetElementsByTagName("current-user-principal")[0];
if (tag != null)
{
XmlNode taghref = tag.ChildNodes[0];
if (taghref != null)
{
string href = taghref.InnerText.Trim();
string baseUrl = "https://caldav.icloud.com" + href;
Console.WriteLine("BASSE URL :" + baseUrl);
content = "<propfind xmlns='DAV:' xmlns:cd='urn:ietf:params:xml:ns:caldav'><prop><cd:calendar-home-set/></prop></propfind>";
ResponseStream = ExectueMethod(username, password, baseUrl, "PROPFIND", null, content, "application/x-www-form-urlencoded","0");
XmlDocument = new XmlDocument();
XmlDocument.Load(ResponseStream);
xmlInner = XmlDocument.InnerXml;
innerXmlDocument = new XmlDocument();
innerXmlDocument.LoadXml(xmlInner);
tag = innerXmlDocument.GetElementsByTagName("calendar-home-set")[0];
if (tag != null)
{
taghref = tag.ChildNodes[0];
if (taghref != null)
{
string calURL = taghref.InnerText.Trim();
Console.WriteLine("CALENDER URL :" + calURL);
content = "<propfind xmlns='DAV:'><prop><displayname/><getctag />calendar-data</prop></propfind>";
ResponseStream = ExectueMethod(username, password, calURL, "PROPFIND", null, content, "application/x-www-form-urlencoded","1");
XmlDocument = new XmlDocument();
XmlDocument.Load(ResponseStream);
xmlInner = XmlDocument.InnerXml;
innerXmlDocument = new XmlDocument();
innerXmlDocument.LoadXml(xmlInner);
int xx = 0;
foreach (XmlNode row in innerXmlDocument.GetElementsByTagName("response"))
{
if (xx > 0)
{
string calhrefUrlfinal = row.InnerXml.Remove(row.InnerXml.IndexOf("</href>"));
calhrefUrlfinal = calhrefUrlfinal.Replace("<href xmlns=\"DAV:\">", "");
string Calname = row.InnerXml.Remove(row.InnerXml.IndexOf("</displayname>"));
Calname = Calname.Remove(0, Calname.IndexOf("<displayname>")).Replace("<displayname>", "").Trim();
string statusx = row.InnerXml.Remove(row.InnerXml.IndexOf("</status>"));
statusx = statusx.Remove(0, statusx.IndexOf("<status>")).Replace("<status>", "").Trim();
if (statusx == "HTTP/1.1 200 OK")
{
Console.Write("THIS IS VALID CAL!");
// ADD THIS CAL TO LIST TO SHOW!
content = "<propfind xmlns='DAV:'><prop><calendar-data/><getctag /></prop></propfind>";
ResponseStream = ExectueMethod(username, password, calhrefUrlfinal, "PROPFIND", null, content, "application/x-www-form-urlencoded", "1");
XmlDocument = new XmlDocument();
XmlDocument.Load(ResponseStream);
xmlInner = XmlDocument.InnerXml;
XmlDocument icsSoc = new XmlDocument();
icsSoc.LoadXml(xmlInner);
foreach (XmlNode ics in icsSoc.GetElementsByTagName("href"))
{
string t = ics.InnerText.Trim();
if (t.Contains(".ics"))
{
DownloadICS(t, Calname + ".ics");
break;
}
}
}
}
xx = xx + 1;
}
}
}
}
}
}
}
}
catch (Exception ex)
{ }
}
private static void DownloadICS(string pathUri, string fileNames)
{
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(username, password);
Byte[] data = request.DownloadData(pathUri);
var str = System.Text.Encoding.Default.GetString(data);
string path = "icloudCalenders\\" + fileNames.Substring(fileNames.LastIndexOf(" / ") + 1);
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Write(data, 0, data.Length);
fs.Close();
}
private static Stream ExectueMethod(string username, string password, string caldevURI, string methodName, WebHeaderCollection headers, string content, string contentType,string depth)
{
HttpWebRequest httpGetRequest = (HttpWebRequest)WebRequest.Create(caldevURI);
httpGetRequest.Credentials = new NetworkCredential(username, password);
httpGetRequest.PreAuthenticate = true;
httpGetRequest.Method = methodName;
httpGetRequest.Headers.Add("Depth", depth);
httpGetRequest.Headers.Add("Authorization", username);
//httpGetRequest.UserAgent = "DAVKit/3.0.6 (661); CalendarStore/3.0.8 (860); iCal/3.0.8 (1287); Mac OS X/10.5.8 (9L31a)";
httpGetRequest.UserAgent = "curl/7.37.0";
if (!string.IsNullOrWhiteSpace(contentType))
{
httpGetRequest.ContentType = contentType;
}
using (var streamWriter = new StreamWriter(httpGetRequest.GetRequestStream()))
{
string data = content;
streamWriter.Write(data);
}
//Stream requestStream = httpGetRequest.GetRequestStream();
//requestStream.Write(optionsArray, 0, optionsArray.Length);
//requestStream.Close();
HttpWebResponse httpGetResponse = (HttpWebResponse)httpGetRequest.GetResponse();
Stream responseStream = httpGetResponse.GetResponseStream();
return responseStream;
}
Actually this is not a question, but an answer for those that are trying to use AWS TranslateText without SDK. I have had many problems until I got a running version.
In my opinion, the AWS documentation for this service is not complete and there are not many examples to check (at least for .Net).
At first, I thought I was not generating the correct V4 signature, but after checking the steps and values once again, I decided to use Postman to call the service. It was very helpful.
Postman can generate the AWS signature! (yeah, I did not know) so finally I noticed the signature value was not the problem.
Checking the request I could see the headers needed and some of its values.
The problem in my case was that I was not sending the "x-amz-target" header.
And by chance I found that the value for this header must be "AWSShineFrontendService_20170701.TranslateText"
(I saw a similar value here https://rdrr.io/cran/aws.translate/src/R/translateHTTP.R)
This other link was also helpful
Ivona Request Signing Issue - signature does not match (AWS Signature Version 4)
So now I have a running version, I want to share my .Net code. I hope it helps :) !!
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web.Script.Serialization;
namespace AWS_TranslateTextTest
{
class AWS_TranslateText
{
// Replace this with your own values
const string AccessKey = "AKI_ADD_YOUR_ACCESSKEY";
const string SecretKey = "ADD_YOUR_SECRETKEY";
static void Main()
{
try
{
string text = "Translate this text from English to German.";
string sourceLang = "en";
string targetLang = "de";
string responseText = TranslateText(text, sourceLang, targetLang);
JObject json = JObject.Parse(responseText);
string translatedText = ""; // to do read response from json or responseText
if (json.ToString().Contains("TranslatedText")){
//To access to the properties in "dot" notation use a dynamic object
dynamic obj = json;
translatedText = obj.TranslatedText.Value;
Console.WriteLine("TranslatedText is: {0}", translatedText);
}
else{
Console.WriteLine("TranslatedText not found in response.");
throw new Exception(json.ToString());
}
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
if (ex.Response != null){
foreach (string header in ex.Response.Headers)
{
Console.WriteLine("{0}: {1}", header, ex.Response.Headers[header]);
}
using (var responseStream = ex.Response.GetResponseStream())
{
if (responseStream != null)
{
using (var streamReader = new StreamReader(responseStream))
{
Console.WriteLine(streamReader.ReadToEnd());
}
}
} }
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static string TranslateText(string text, string sourceLang, string targetLang)
{
var date = DateTime.UtcNow;
const string algorithm = "AWS4-HMAC-SHA256";
const string regionName = "eu-west-1";
const string serviceName = "translate";
const string method = "POST";
const string canonicalUri = "/";
const string canonicalQueryString = "";
const string x_amz_target_header = "AWSShineFrontendService_20170701.TranslateText";
const string contentType = "application/x-amz-json-1.1";
const string host = serviceName + "." + regionName + ".amazonaws.com";
var obj = new
{
SourceLanguageCode = sourceLang,
TargetLanguageCode = targetLang,
Text = text
};
var requestPayload = new JavaScriptSerializer().Serialize(obj);
var hashedRequestPayload = HexEncode(Hash(ToBytes(requestPayload)));
var dateStamp = date.ToString("yyyyMMdd");
var requestDate = date.ToString("yyyyMMddTHHmmss") + "Z";
var credentialScope = string.Format("{0}/{1}/{2}/aws4_request", dateStamp, regionName, serviceName);
var bytes = ToBytes(requestPayload);
var headers = new SortedDictionary<string, string>
{
{"content-length", bytes.Length.ToString()},
{"content-type", contentType},
{"host", host},
{"x-amz-date", requestDate},
{"x-amz-target", x_amz_target_header}
};
string canonicalHeaders =
string.Join("\n", headers.Select(x => x.Key.ToLowerInvariant() + ":" + x.Value.Trim())) + "\n";
string signedHeaders =
string.Join(";", headers.Select(x => x.Key.ToLowerInvariant() ));
// Task 1: Create a Canonical Request For Signature Version 4
var canonicalRequest = method + '\n' + canonicalUri + '\n' + canonicalQueryString +
'\n' + canonicalHeaders + '\n' + signedHeaders + '\n' + hashedRequestPayload;
var hashedCanonicalRequest = HexEncode(Hash(ToBytes(canonicalRequest)));
// Task 2: Create a String to Sign for Signature Version 4
// StringToSign = Algorithm + '\n' + RequestDate + '\n' + CredentialScope + '\n' + HashedCanonicalRequest
var stringToSign = string.Format("{0}\n{1}\n{2}\n{3}", algorithm, requestDate, credentialScope,
hashedCanonicalRequest);
// Task 3: Calculate the AWS Signature Version 4
// HMAC(HMAC(HMAC(HMAC("AWS4" + kSecret,"20130913"),"eu-west-1"),"tts"),"aws4_request")
byte[] signingKey = GetSignatureKey(SecretKey, dateStamp, regionName, serviceName);
// signature = HexEncode(HMAC(derived-signing-key, string-to-sign))
var signature = HexEncode(HmacSha256(stringToSign, signingKey));
// Task 4: Prepare a signed request
// Authorization: algorithm Credential=access key ID/credential scope, SignedHeadaers=SignedHeaders, Signature=signature
var authorization =
string.Format("{0} Credential={1}/{2}/{3}/{4}/aws4_request, SignedHeaders={5}, Signature={6}",
algorithm, AccessKey, dateStamp, regionName, serviceName, signedHeaders, signature);
// Send the request
string endpoint = "https://" + host; // + canonicalUri ;
var webRequest = WebRequest.Create(endpoint);
webRequest.Method = method;
webRequest.Timeout = 20000;
webRequest.ContentType = contentType;
webRequest.Headers.Add("X-Amz-Date", requestDate);
webRequest.Headers.Add("Authorization", authorization);
webRequest.Headers.Add("X-Amz-Target", x_amz_target_header);
webRequest.ContentLength = bytes.Length;
using (Stream newStream = webRequest.GetRequestStream())
{
newStream.Write(bytes, 0, bytes.Length);
newStream.Flush();
}
var response = (HttpWebResponse)webRequest.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (var streamReader = new StreamReader(responseStream))
{
string res = streamReader.ReadToEnd();
return res;
}
}
}
return null;
}
private static byte[] GetSignatureKey(String key, String dateStamp, String regionName, String serviceName)
{
byte[] kDate = HmacSha256(dateStamp, ToBytes("AWS4" + key));
byte[] kRegion = HmacSha256(regionName, kDate);
byte[] kService = HmacSha256(serviceName, kRegion);
return HmacSha256("aws4_request", kService);
}
private static byte[] ToBytes(string str)
{
return Encoding.UTF8.GetBytes(str.ToCharArray());
}
private static string HexEncode(byte[] bytes)
{
return BitConverter.ToString(bytes).Replace("-", string.Empty).ToLowerInvariant();
}
private static byte[] Hash(byte[] bytes)
{
return SHA256.Create().ComputeHash(bytes);
}
private static byte[] HmacSha256(String data, byte[] key)
{
return new HMACSHA256(key).ComputeHash(ToBytes(data));
}
}
}
The following method generate a XmlDocument from a string and then call another method to generate a hash of the XmlDocument created.
private void geraXML()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
string xml = #"<?xml version=""1.0""?>...";
xmlDoc.LoadXml(xml);
string caminho = path/+"xmldoc.xml";
string nomeArquivo = "xmldoc.xml";
xmlDoc.Save(caminho);
//call method to generate hash
geraHASH(caminho, nomeArquivo);
}
This another method convert the same string of first method to stream and send to a webService
private void enviaACCS001(string protocolo, string caminho)
{
string base64 = Convert.ToBase64String(Encoding.Default.GetBytes("user:password"));
string authorization = String.Concat("Basic ", base64);
String finalResult;
HttpWebRequest hwrRequest = (HttpWebRequest)HttpWebRequest.Create("addres/" + protocolo);
hwrRequest.UseDefaultCredentials = true;
hwrRequest.Headers.Add("Authorization", authorization);
hwrRequest.Method = "PUT";
string finalXML = #"<?xml version=""1.0""?>...";
byte[] bytes = Encoding.Default.GetBytes(finalXML);
hwrRequest.ContentLength = bytes.Length;
using (Stream putStream = hwrRequest.GetRequestStream())
{
putStream.Write(bytes, 0, bytes.Length);
}
using (HttpWebResponse response = (HttpWebResponse)hwrRequest.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
finalResult = reader.ReadToEnd();
visualiza.Text = visualiza.Text + "\n " + finalResult;
}
The WebService generate the hash of stream and compare with the first hash generated. So far so good, the compare return true, but the WebService need the encoding="UTF-16BE" and when insert this information into the string the hash not match. What i'm doing wrong?
First method with encoding="UTF-16BE"
private void geraXML()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
string xml = #"<?xml version=""1.0"" encoding=""UTF-16BE""?>...";
xmlDoc.LoadXml(xml);
string caminho = path/+"xmldoc.xml";
string nomeArquivo = "xmldoc.xml";
xmlDoc.Save(caminho);
//call method to generate hash
geraHASH(caminho, nomeArquivo);
}
Second method with encoding="utf-16bE"
private void enviaACCS001(string protocolo, string caminho)
{
string base64 = Convert.ToBase64String(Encoding.Default.GetBytes("user:password"));
string authorization = String.Concat("Basic ", base64);
String finalResult;
HttpWebRequest hwrRequest = (HttpWebRequest)HttpWebRequest.Create("addres/" + protocolo);
hwrRequest.UseDefaultCredentials = true;
hwrRequest.Headers.Add("Authorization", authorization);
hwrRequest.Method = "PUT";
string finalXML = #"<?xml version=""1.0"" encoding=""UTF-16BE""?>...";
byte[] bytes = Encoding.BigEndianUnicode.GetBytes(finalXML);
hwrRequest.ContentLength = bytes.Length;
using (Stream putStream = hwrRequest.GetRequestStream())
{
putStream.Write(bytes, 0, bytes.Length);
}
using (HttpWebResponse response = (HttpWebResponse)hwrRequest.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
finalResult = reader.ReadToEnd();
visualiza.Text = visualiza.Text + "\n " + finalResult;
}
Can anyone please provide a CALDAV example for C#? Thanks in advance. I am struggling to find an example except this one.
https://ovaismehboob.wordpress.com/2014/03/30/sync-calendar-events-using-caldav/
But id does not work for me.
I get the following Error.
{"The remote server returned an error: (400) Bad Request."}
Here is the code,
public class Caldevx
{
static System.IO.Stream ResponseStream;
static System.Xml.XmlDocument XmlDocument;
static string calendarURI = "https://caldav.calendar.yahoo.com/dav/";
static string username = "thanujastech#yahoo.com";
static string password = "******";
static string content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><propfind xlmns\"DAV:\"><allprop/></propfind>";
public void GetCalendars(){
WebHeaderCollection whc = new WebHeaderCollection();
whc.Add(#"Translate", "F");
ResponseStream = ExectueMethod(username, password, calendarURI,"PROPFIND", whc, content, "text/xml");
XmlDocument = new XmlDocument();
XmlDocument.Load(ResponseStream);
string xmlInner = XmlDocument.InnerXml;
XmlDocument innerXmlDocument = new XmlDocument();
innerXmlDocument.LoadXml(xmlInner);
XmlNodeList lst = innerXmlDocument.GetElementsByTagName("DAV:href");
List<string> lstICSs = new List<string>();
foreach(XmlNode node in lst){
if(node.InnerText.EndsWith(".ics"))
{
lstICSs.Add(node.InnerText.Trim());
}
ResponseStream.Close();
if (lstICSs.Count > 0)
{
DownloadICS("https//caldav.calendar.yahoo.com", lstICSs);
}
}
}
private void DownloadICS(string pathUri, List<string> fileNames)
{
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(username, password);
foreach (var file in fileNames)
{
Byte[] data = request.DownloadData(pathUri + file);
var str = System.Text.Encoding.Default.GetString(data);
string path = #"C:\" + file.Substring(file.LastIndexOf("/")+1);
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Write(data, 0, data.Length);
fs.Close();
DDay.iCal.IICalendarCollection calendars = iCalendar.LoadFromFile(path);
foreach (var item in calendars)
{
foreach (Event e in item.Events)
{
Console.WriteLine(e.Description);
}
}
}
}
private Stream ExectueMethod(string username, string password, string caldevURI, string methodName, WebHeaderCollection headers, string content, string contentType)
{
HttpWebRequest httpGetRequest = (HttpWebRequest)WebRequest.Create(caldevURI);
httpGetRequest.Credentials = new NetworkCredential(username, password);
httpGetRequest.PreAuthenticate = true;
httpGetRequest.Method = methodName;
if (headers != null && headers.HasKeys())
{
httpGetRequest.Headers = headers;
}
byte[] optionsArray = Encoding.UTF8.GetBytes(content);
httpGetRequest.ContentLength = optionsArray.Length;
if (!string.IsNullOrWhiteSpace(contentType))
{
httpGetRequest.ContentType = contentType;
}
Stream requestStream = httpGetRequest.GetRequestStream();
requestStream.Write(optionsArray, 0, optionsArray.Length);
requestStream.Close();
HttpWebResponse httpGetResponse = (HttpWebResponse)httpGetRequest.GetResponse();
Stream responseStream = httpGetResponse.GetResponseStream();
return ResponseStream;
}
}
HI i have a webservice IIS with this code:
// Implements multipart/form-data POST in C# http://www.ietf.org/rfc/rfc2388.txt
// http://www.briangrinstead.com/blog/multipart-form-post-in-c
public static class FormUpload
{
private static readonly Encoding encoding = Encoding.UTF8;
public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters)
{
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
string contentType = "multipart/form-data; boundary=" + formDataBoundary;
byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);
return PostForm(postUrl, userAgent, contentType, formData);
}
private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData)
{
HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
if (request == null)
{
throw new NullReferenceException("request is not a http request");
}
// Set up the request properties.
request.Method = "POST";
request.ContentType = contentType;
request.UserAgent = userAgent;
request.CookieContainer = new CookieContainer();
request.ContentLength = formData.Length;
// You could add authentication here as well if needed:
// request.PreAuthenticate = true;
// request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
// request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes("username" + ":" + "password")));
// Send the form data to the request.
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(formData, 0, formData.Length);
requestStream.Close();
}
return request.GetResponse() as HttpWebResponse;
}
private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
{
Stream formDataStream = new System.IO.MemoryStream();
bool needsCLRF = false;
foreach (var param in postParameters)
{
// Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
// Skip it on the first parameter, add it to subsequent parameters.
if (needsCLRF)
formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));
needsCLRF = true;
if (param.Value is FileParameter)
{
FileParameter fileToUpload = (FileParameter)param.Value;
// Add just the first part of this param, since we will write the file data directly to the Stream
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
boundary,
param.Key,
fileToUpload.FileName ?? param.Key,
fileToUpload.ContentType ?? "application/octet-stream");
formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));
// Write the file data directly to the Stream, rather than serializing it to a string.
formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);
}
else
{
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
boundary,
param.Key,
param.Value);
formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
}
}
// Add the end of the request. Start with a newline
string footer = "\r\n--" + boundary + "--\r\n";
formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));
// Dump the Stream into a byte[]
formDataStream.Position = 0;
byte[] formData = new byte[formDataStream.Length];
formDataStream.Read(formData, 0, formData.Length);
formDataStream.Close();
return formData;
}
public class FileParameter
{
public byte[] File { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public FileParameter(byte[] file) : this(file, null) { }
public FileParameter(byte[] file, string filename) : this(file, filename, null) { }
public FileParameter(byte[] file, string filename, string contenttype)
{
File = file;
FileName = filename;
ContentType = contenttype;
}
}
}
Obtain from here: Multipart forms from C# client
But now, in mi android aplicattion i have this:
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 300000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 300000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost postRequest = new HttpPost("http://172.21.1.87:9999/Service1.svc");
ByteArrayBody bab = new ByteArrayBody(ficheroAEnviar, "prueba.jpg");
MultipartEntity reqEntity = new MultipartEntity();
postRequest.addHeader("Content-Type", " multipart/form-data");
reqEntity.addPart("Dictionary", new FileBody(new File(fileUri.toString(), "application/zip")));
reqEntity.addPart("boundary", new StringBody("envio"));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
postRequest.getAllHeaders();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
}
catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
But it return me Bad request 400 error. Can i use the c# code to upload a multipartentity file with android or a need to make any change in some code?
Anyone have a example en c# and android to make this? Is for uppload videos about 10 or 15 MB.
Thanks
dont use multipart entity instead use as here you define the bounderies it is easy
try it
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "/data/file_to_send.mp3";
String urlServer = "http://192.168.1.1/handle_upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""+pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}
courtesy rafel