HttpWebRequest httpclient = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/v2/activities/addcontacts?api_key=[REDACTED]"); string boundary = "--" + Guid.NewGuid().ToString(); string filename = "F:\\constant contact Wrapper\\Constant.csv"; httpclient.Headers.Add("Authorization", "Bearer [REDACTED]"); // httpclient.Headers.Add("Accept", "application/json"); // httpclient.Headers.Add("content-type", "multipart/form-data"); // File fileToUse = new File(); // FileBody data = new FileBody(fileToUse); httpclient.Method = "POST"; httpclient.ContentType = string.Format("multipart/form-data"); httpclient.Accept = "application/json"; httpclient.PreAuthenticate = true; //string filename = "C:\\Development\\constantcontact\\emailkust.csv"; StringBuilder sb= new StringBuilder(); sb.AppendLine(boundary); sb.AppendLine("content-disposition: form-data; name="+filename +""); sb.AppendLine(); sb.AppendLine("Constant.csv"); //'filename sb.AppendLine(boundary); sb.AppendLine("content-disposition: form-data; name=" + filename + ""); //sb.AppendLine("content-disposition: form-data");//; name=""lists"""); sb.AppendLine(); sb.AppendLine("1") ;//' List Number sb.AppendLine(boundary); sb.AppendLine("content-disposition: form-data; name=" + filename + ""); // sb.AppendLine("content-disposition: file"); // sb.AppendLine("content-disposition: file; name=""data"" filename=""emailkust.csv"""); //sb.AppendLine("content-disposition: form-data; filename=Constant.csv"); sb.AppendLine("Content-Type: text/csv"); sb.AppendLine(); using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { byte[] contents = new byte[fs.Length]; fs.Read(contents, 0, contents.Length); sb.Append(Encoding.Default.GetString(contents)); } sb.AppendLine(boundary ); byte[] fulldata = Encoding.Default.GetBytes(sb.ToString()); // httpclient.ContentLength == fulldata.Length; using (Stream sw = httpclient.GetRequestStream()) { sw.Write(fulldata, 0, fulldata.Length); } try { HttpWebResponse response = httpclient.GetResponse() as HttpWebResponse; using (StreamReader sr = new StreamReader(response.GetResponseStream())) { //MessageBox.Show(sr.ReadToEnd()); } } catch (WebException ex) { using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream())) { //MessageBox.Show("Error Message:" + e.Message + Constants.vbCr + Constants.vbLf + "Error Body: " + sr.ReadToEnd(), "Web Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
i got an error in that code:The remote server returned an error: (400) Bad Request.
please provide the my csv file is correct or not an if i can change my code?
... View more