Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de8cccd
commit 4fc9762
Showing
10 changed files
with
162 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MarkDownEditor | ||
{ | ||
public class SMMSClient | ||
{ | ||
public static string UploadImageToSMMS(string path) | ||
{ | ||
FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read); | ||
|
||
using (var client = new HttpClient()) | ||
using (var formData = new MultipartFormDataContent()) | ||
{ | ||
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml"); | ||
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0"); | ||
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1"); | ||
|
||
|
||
|
||
HttpContent fileStreamContent = new StreamContent(fs); | ||
fileStreamContent.Headers.Add("Content-Type", "multipart/form-data"); | ||
formData.Add(fileStreamContent, "smfile", path); | ||
var res = client.PostAsync("https://sm.ms/api/upload", formData).Result; | ||
|
||
if (!res.IsSuccessStatusCode) | ||
{ | ||
throw new HttpRequestException("Net error!"); | ||
} | ||
|
||
using (var streamReader = new StreamReader(res.Content.ReadAsStreamAsync().Result)) | ||
{ | ||
JObject o = JObject.Parse(streamReader.ReadToEnd()); | ||
if (o["code"].ToString().ToLower() != "success") | ||
throw new HttpRequestException(o["msg"].ToString()); | ||
|
||
return o["data"]["url"].ToString(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters