protected MultipartEntity CreateMultipartBody <T>(T entity, string attachmentName, string attachmentPath) where T : BaseParameter { MultipartEntity result = new MultipartEntity(); var kvs = ResolveParameter(entity); foreach (var kv in kvs) { result.AddBody(new StringBody(Encoding.UTF8, kv.Key, kv.Value)); } if (!string.IsNullOrEmpty(attachmentPath)) { FileInfo fileInfo = new FileInfo(attachmentPath); var fileName = Path.GetFileName(attachmentPath); if (string.IsNullOrEmpty(attachmentName)) { attachmentName = fileName; } UTF8FileBody fileBody = new UTF8FileBody(attachmentName, fileName, fileInfo); result.AddBody(fileBody); } return(result); }
public void HttpPostWithFilesAndParameters() { // this method sends an empty file (or no file :) HttpClient client = new HttpClient(); HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_MULTIPART_POST_200)); MultipartEntity multipartEntity = new MultipartEntity(); StringBody stringBody1 = new StringBody(Encoding.ASCII, "param1", "value1"); multipartEntity.AddBody(stringBody1); StringBody stringBody2 = new StringBody(Encoding.ASCII, "param2", "!#$^&*((<>"); multipartEntity.AddBody(stringBody2); FileBody fileBody1 = new FileBody("photo", "me.file", null); multipartEntity.AddBody(fileBody1); postMethod.Entity = multipartEntity; HttpResponse response = client.Execute(postMethod); Assert.AreEqual(200, response.ResponseCode); string responseString = EntityUtils.ToString(response.Entity); MessageData md = new MessageData(); md.PostParameters.Add(new NameValuePair("param1", "value1")); md.PostParameters.Add(new NameValuePair("param2", "!#$^&*((<>")); md.Files.Add(new NameValuePair("me.file", "0")); Assert.AreEqual(md.ToString(), responseString); Assert.AreEqual(Constants.HTTP_MULTIPART_POST_200, response.RequestUri.AbsoluteUri); Console.Write(responseString); }
public static string get_9kw_api_upload(string data) { HttpClient client = new HttpClient(); HttpPost postMethod = new HttpPost(new Uri("http://www.9kw.eu/index.cgi")); MultipartEntity multipartEntity = new MultipartEntity(); postMethod.Entity = multipartEntity; StringBody stringBody = new StringBody(Encoding.UTF8, "apikey", _9kw.apikey); multipartEntity.AddBody(stringBody); StringBody stringBody3 = new StringBody(Encoding.UTF8, "source", "csapi"); multipartEntity.AddBody(stringBody3); StringBody stringBody2 = new StringBody(Encoding.UTF8, "action", "usercaptchaupload"); multipartEntity.AddBody(stringBody2); FileInfo fileInfo = new FileInfo(@data); FileBody fileBody = new FileBody("file-upload-01", data, fileInfo); multipartEntity.AddBody(fileBody); HttpResponse response = client.Execute(postMethod); return(EntityUtils.ToString(response.Entity)); }
public void HttpPostWithFilesAndParameters3() { // this method sends a file and checks for the number of bytes recieved HttpClient client = new HttpClient(); string url = "http://www.fiddler2.com/sandbox/FileForm.asp"; HttpPost postMethod = new HttpPost(new Uri(url)); MultipartEntity multipartEntity = new MultipartEntity(); postMethod.Entity = multipartEntity; string fileName = "small-text.txt"; StringBody stringBody1 = new StringBody(Encoding.ASCII, "1", "1_"); multipartEntity.AddBody(stringBody1); FileInfo fi = ResourceManager.GetResourceFileInfo(fileName); FileBody fileBody1 = new FileBody("fileentry", fileName, fi, "text/plain"); multipartEntity.AddBody(fileBody1); StringBody stringBody2 = new StringBody(Encoding.ASCII, "_charset_", "windows-1252"); multipartEntity.AddBody(stringBody2); HttpResponse response = client.Execute(postMethod); Assert.AreEqual(200, response.ResponseCode); string responseString = EntityUtils.ToString(response.Entity); Assert.AreEqual(url, response.RequestUri.AbsoluteUri); Console.Write(responseString); }
private void AttachFiles(ISendGrid message, MultipartEntity multipartEntity) { var files = FetchFileBodies(message); files.ForEach(kvp => multipartEntity.AddBody(new FileBody("files[" + Path.GetFileName(kvp.Key) + "]", Path.GetFileName(kvp.Key), kvp.Value))); var streamingFiles = FetchStreamingFileBodies(message); streamingFiles.ForEach(kvp => multipartEntity.AddBody(new StreamedFileBody(kvp.Value, kvp.Key))); }
/// <summary> /// Old style login. /// Don't know on which devices this is actually used. /// </summary> private void DoOldLogin() { var url = new UriBuilder("https", deviceIp, DevicePort, "/cgi-bin/login.cgi").Uri; var loginEntity = new MultipartEntity(); loginEntity.AddBody(new StringBody(PartEncoding, "user", UserName)); loginEntity.AddBody(new StringBody(PartEncoding, "passwd", password)); httpClient.Post(url, loginEntity); }
public static void SendWelcomeMail(string userName, string email) { HttpClient client = new HttpClient(); HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json")); MultipartEntity multipartEntity = new MultipartEntity(); postMethod.Entity = multipartEntity; multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", "superjokes_register")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "substitution_vars", "{\"to\": [\"" + email + "\"], \"sub\" : { \"%username%\" : [\"" + userName + "\"]}}")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", "superjokes_cn")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", AppConfig.SendCloudKey)); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", "*****@*****.**")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", "超级冷笑话")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", "超级冷笑话注册邮件")); CodeScales.Http.Methods.HttpResponse response = client.Execute(postMethod); var repCode = response.ResponseCode; var repResult = EntityUtils.ToString(response.Entity); //LogHelper.Info("reg:" + email + repResult + AppConfig.SendCloudKey); //Console.WriteLine("Response Code: " + response.ResponseCode); //Console.WriteLine("Response Content: " + EntityUtils.ToString(response.Entity)); //Response.Write("Response Code: " + response.ResponseCode); //Response.Write("<br/>"); //Response.Write("Response Content: " + EntityUtils.ToString(response.Entity)); }
/// <summary> /// 邮件发送 /// </summary> /// <param name="to"></param> /// <param name="subject"></param> /// <param name="Content"></param> private static void SendMail(string to, string subject, string Content) { HttpClient client = new HttpClient(); HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send.xml")); MultipartEntity multipartEntity = new MultipartEntity(); postMethod.Entity = multipartEntity; // 不同于登录SendCloud站点的帐号,您需要登录后台创建发信域名,获得对应发信域名下的帐号和密码才可以进行邮件的发送。 multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", "*****@*****.**")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", "SdCc6gvb")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", "*****@*****.**")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", "XBA游戏网")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "to", to)); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", subject)); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "html", Content)); //FileInfo fileInfo = new FileInfo(@"c:\SANCH5890V.jpg"); //UTF8FileBody fileBody = new UTF8FileBody("file1", "SANCH5890V.jpg", fileInfo); //multipartEntity.AddBody(fileBody); HttpResponse response = client.Execute(postMethod); if (response.ResponseCode == 200) { LogMsg(" 发送邮件给:" + to + " 请求状态:" + response.ResponseCode + " 发送状态:" + EntityUtils.ToString(response.Entity).Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r", "")); } else { LogMsg(" 发送邮件给:" + to + " 请求状态:" + response.ResponseCode + " 发送状态:" + EntityUtils.ToString(response.Entity).Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r", "")); } //Console.WriteLine("Response Code: " + response.ResponseCode); //Console.WriteLine("Response Content: " + EntityUtils.ToString(response.Entity)); }
public string postFile(string session, string fileName) { int GOOD_RETURN_CODE = 200; string subIdLocal = ""; FileStream imageFile = File.OpenRead(fileName); CodeScales.Http.HttpClient httpClient = new CodeScales.Http.HttpClient(); HttpPost httpPost = new HttpPost(new Uri(url + "/api/upload/")); JsonObject jsonObject = getUploadJSON(session); MultipartEntity reqEntity = new MultipartEntity(); StringBody stringBody = new StringBody(Encoding.UTF8, "request-json", jsonObject.ToString()); reqEntity.AddBody(stringBody); FileInfo fileInfo = new FileInfo(fileName); FileBody fileBody = new FileBody("file", fileName, fileInfo); reqEntity.AddBody(fileBody); httpPost.Entity = reqEntity; HttpResponse response = httpClient.Execute(httpPost); String result = ""; int respCode = response.ResponseCode; if (respCode == GOOD_RETURN_CODE) { HttpEntity entity = response.Entity; String content = EntityUtils.ToString(entity); string success = jsonGetValue(content, "status"); subIdLocal = jsonGetValue(content, "subid"); string hash = jsonGetValue(content, "hash"); } return(subIdLocal); }
/// <summary> /// Add parts to the entity. /// </summary> private void AddParts(MultipartEntity entity, string command, string barFilePath, bool includePackageId, bool includePackageName, string packageId) { entity.AddBody(new StringBody(PartEncoding, "command", command)); if (barFilePath != null) { entity.AddBody(new FileBody("file", Path.GetFileName(barFilePath), new FileInfo(barFilePath), "application/zip")); } if (includePackageId || includePackageName) { string name = null; if (packageId == null) { if (barFilePath == null) { throw new ArgumentNullException("barFilePath"); } var barFile = new BarFile(barFilePath); packageId = barFile.Manifest.PackageId; name = barFile.Manifest.PackageName; } if (includePackageId) { if (packageId == null) { throw new ArgumentException("Missing Package-Id"); } entity.AddBody(new StringBody(PartEncoding, "package_id", packageId)); } if (includePackageName || (name != null)) { if (name == null) { throw new ArgumentException("Missing Package-Name"); } entity.AddBody(new StringBody(PartEncoding, "package_name", name)); } } }
public void HttpPostWithFilesAndParameters2() { // this method sends a file and checks for the number of bytes recieved HttpClient client = new HttpClient(); HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_MULTIPART_POST_200)); MultipartEntity multipartEntity = new MultipartEntity(); string fileName = "big-text.txt"; FileInfo fi = ResourceManager.GetResourceFileInfo(fileName); FileBody fileBody1 = new FileBody("file", fileName, fi, "text/plain"); multipartEntity.AddBody(fileBody1); postMethod.Entity = multipartEntity; StringBody stringBody1 = new StringBody(Encoding.ASCII, "param1", "value1"); multipartEntity.AddBody(stringBody1); StringBody stringBody2 = new StringBody(Encoding.ASCII, "param2", "!#$^&*((<>"); multipartEntity.AddBody(stringBody2); HttpResponse response = client.Execute(postMethod); Assert.AreEqual(200, response.ResponseCode); string responseString = EntityUtils.ToString(response.Entity); MessageData md = new MessageData(); md.PostParameters.Add(new NameValuePair("param1", "value1")); md.PostParameters.Add(new NameValuePair("param2", "!#$^&*((<>")); md.Files.Add(new NameValuePair(fileName, fi.Length.ToString())); Assert.AreEqual(md.ToString(), responseString); Assert.AreEqual(Constants.HTTP_MULTIPART_POST_200, response.RequestUri.AbsoluteUri); Console.Write(responseString); }
public static void VerifyNotice(string userName, string email, string jokeTitle, string jokeurl) { HttpClient client = new HttpClient(); HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json")); MultipartEntity multipartEntity = new MultipartEntity(); postMethod.Entity = multipartEntity; multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", "superjokes_verifynotice")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "substitution_vars", "{\"to\": [\"" + email + "\"], \"sub\" : { \"%username%\" : [\"" + userName + "\"],\"%joketitle%\":[\"" + jokeTitle + "\"],\"%jokeurl%\":[\"" + jokeurl + "\"]}}")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", "superjokes_cn")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", AppConfig.SendCloudKey)); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", "*****@*****.**")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", "超级冷笑话")); multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", "超级冷笑话审核通知")); CodeScales.Http.Methods.HttpResponse response = client.Execute(postMethod); var repCode = response.ResponseCode; var repResult = EntityUtils.ToString(response.Entity); //LogHelper.Info("verify:" + email + repResult + AppConfig.SendCloudKey); }
private void AttachFormParams(ISendGrid message, MultipartEntity multipartEntity) { var formParams = FetchFormParams(message); formParams.ForEach(kvp => multipartEntity.AddBody(new StringBody(Encoding.UTF8, kvp.Key, kvp.Value))); }
private void AttachFiles(ISendGrid message, MultipartEntity multipartEntity) { var files = FetchFileBodies(message); files.ForEach(kvp => multipartEntity.AddBody(new FileBody("files[" + kvp.Key + "]", kvp.Key, kvp.Value))); }