Hi all,
I need to send a csv file to a https site. The instructions I have been given to talk to the https have cURL commands so I am trying to duplicate this. I'm am trying to via httpwebrequest and httpWebReponse but I can't get it to work. I have based it on the ftpwebrequest / response from Axaptapedia but the actual send of the binary data has caused an error.
the cURL instuctions are:-
curl --request POST \
--header 'Authorization: bearer tokenforauthorisation' \
--header 'Content-type: text/csv' \
--header 'Accept: text/plain' \
--data-binary @/myhome/salesdata-20160512.csv \
https://a_url_to_post_to/api
to emulate this I have used the following code (currently as a job) but I get an error 415 "unsupported Media Type" when I try to get the response - debugger error appears on line = webreq.GetResponse();
Any pointers would be greatfully accepted as I'm running out of time to get this live.
Thanks
Paulina
private void SendToAPI_PT()
{
System.Net.HttpWebRequest webReq;
System.Net.HttpWebResponse webRes;
System.Exception ex,webexception;
CLRObject clrObj,httpresponse;
System.IO.Stream stream;
InteropPermission permission;
System.IO.StreamReader streamRead;
System.IO.StreamWriter streamWrite;
System.Net.ServicePoint servicePt;
System.Text.Encoding utf8;
System.String filecontents;
System.IO.StreamReader reader;
System.Byte[] bytes;
FilePath file;
System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
System.Net.Authorization bearer = new System.Net.Authorization('Log_access_code');
file = @"C:\users\paulina\salesdata_blank.csv";
// Read file
reader = new System.IO.StreamReader(file);
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes( reader.ReadToEnd() );
reader.Close();
//This line gives the user control to run unmanaged and managed code
new InteropPermission(InteropKind::ClrInterop).assert();
// Create Object for adding headers
headers = new System.Net.WebHeaderCollection();
clrObj = System.Net.WebRequest::Create('https://a_url_to_post_to/api');
webReq = clrObj;
//Set Method Type
webReq.set_Method('POST');
webReq.set_KeepAlive(true);
// Set Content type
webReq.set_ContentType('text/csv');
webReq.set_Accept('text/plain');
webReq.set_ContentLength(bytes.get_Length());
// Add Authorization code
headers.Add('Authorization: bearer tokenforauthorisation');
//Add header to request
webReq.set_Headers(headers);
//Get Service Point
servicePt = webReq.get_ServicePoint();
servicePt.set_Expect100Continue(false);
//Gets the response object
try
{
// calling [Begin]GetRequestStream before [Begin]GetResponse.
Stream = webreq.GetRequestStream();
Stream.Write(bytes,0,bytes.get_Length());
Stream.Close();
webres = webreq.GetResponse();
info(webres.get_StatusDescription());
webRes.Close();
}
catch (Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(ex.ToString());
if (ex != null)
{
ex = ex.get_InnerException();
if ((ex != null) && (ex is System.Net.WebException))
{
webException = ex as System.Net.WebException;
info(webexception.ToString());
}
}
}
}