Created
June 17, 2013 08:20
-
-
Save borgle/5795401 to your computer and use it in GitHub Desktop.
利用BasicAuthorization方式直接调用接口发送微博,无需采用oauth方式需要不停的授权活动用access_token来做。
This file contains hidden or 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
namespace Utils | |
{ | |
/* | |
Dictionary<string, string> texts = new Dictionary<string, string>(); | |
texts.Add("source", "app key"); | |
texts.Add("status", "post weibo test " + DateTime.Now.ToString()); | |
texts.Add("visible", "2"); | |
Dictionary<string, string> files = new Dictionary<string, string>(); | |
files.Add("G:\\Desktop\\wiki_logo.png", "image/png"); | |
string json_result = Utils.PostUtils.Post2Weibo(texts, files); | |
*/ | |
using System; | |
using System.IO; | |
using System.Text; | |
using System.Net; | |
public class PostUtils | |
{ | |
public static string Post2Weibo(Dictionary<string, string> texts, Dictionary<string, string> files) | |
{ | |
string username = "weibo_username"; | |
string password = "weibo_password"; | |
string url = "https://upload.api.weibo.com/2/statuses/upload.json"; | |
string boundary = "--------------------------------YokerWu"; | |
byte[] boundarybytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); | |
Uri myUri = new Uri(url); | |
WebRequest myWebRequest = HttpWebRequest.Create(myUri); | |
NetworkCredential myNetworkCredential = new NetworkCredential(username, password); | |
CredentialCache myCredentialCache = new CredentialCache(); | |
myCredentialCache.Add(myUri, "Basic", myNetworkCredential); | |
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(myUri); | |
request.Method = "POST"; | |
request.ContentType = "multipart/form-data; boundary=" + boundary; | |
request.Accept = "*/*"; | |
request.Timeout = 15000; | |
request.PreAuthenticate = true; | |
request.Credentials = myCredentialCache; | |
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password))); | |
Stream requestStream = null; | |
WebResponse response = null; | |
string responseStr = null; | |
try | |
{ | |
requestStream = request.GetRequestStream(); | |
#region 提交文本字段数据 | |
string tpl_text = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; | |
foreach (string key in texts.Keys) | |
{ | |
string value = texts[key]; | |
if (value == null) { continue; } | |
byte[] textbytes = Encoding.UTF8.GetBytes(string.Format(tpl_text, key, value)); | |
requestStream.Write(boundarybytes, 0, boundarybytes.Length); | |
requestStream.Write(textbytes, 0, textbytes.Length); | |
} | |
#endregion | |
#region 提交文件数据流 | |
string tpl_file = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; | |
foreach (string filename in files.Keys) | |
{ | |
string content_type = files[filename]; | |
byte[] filebytes = Encoding.UTF8.GetBytes(string.Format(tpl_file, "pic", filename, content_type)); | |
requestStream.Write(boundarybytes, 0, boundarybytes.Length); | |
requestStream.Write(filebytes, 0, filebytes.Length); | |
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read); | |
byte[] buffer = new byte[4096]; | |
int bytesRead = 0; | |
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) | |
{ | |
requestStream.Write(buffer, 0, bytesRead); | |
} | |
fileStream.Close(); | |
} | |
#endregion | |
#region 提交结尾数据包 | |
byte[] footerbytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); | |
requestStream.Write(footerbytes, 0, footerbytes.Length); | |
#endregion | |
requestStream.Close(); | |
response = request.GetResponse(); | |
if (response != null) | |
{ | |
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); | |
responseStr = reader.ReadToEnd(); | |
reader.Close(); | |
} | |
} | |
catch (Exception) | |
{ | |
throw; | |
} | |
finally | |
{ | |
request = null; | |
requestStream = null; | |
response = null; | |
} | |
return responseStr; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment