微信開発者-アクティブリクエスト-準備作業-(1)get、postリクエストを送信し、ファイルをアップロード(C#)
4300 ワード
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
/// <summary>
/// RequestHelper
/// </summary>
public class RequestHelper
{
// get
public static string sendGet(string url)
{
MemoryStream content = new MemoryStream();
// url Web
HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
if (webRequest != null)
{
//
using (WebResponse response = webRequest.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
return reader.ReadToEnd();
}
}
}
}
return "";
}
// post
public static string sendPost(string url, string data)
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
Encoding encoding = Encoding.UTF8;
string param = data;
//encoding.GetBytes(postData);
byte[] bs = encoding.GetBytes(data); //Encoding.ASCII.GetBytes(param);
string responseData = String.Empty;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
responseData = reader.ReadToEnd().ToString();
return responseData;
}
}
}
//
public static String uploadFile(String url, byte[] file, String contenttype, String filename)
{
byte[] buffer = file;
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
//
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "multipart/form-data; boundary=" + boundary;
//
StringBuilder sb = new StringBuilder();
sb.Append("--" + boundary + "\r
");
sb.Append("Content-Disposition: form-data; name=\"media\"; filename=\"" + filename + "\"; filelength=\"" + buffer.Length + "\"");
sb.Append("\r
");
sb.Append("Content-Type: " + contenttype);
sb.Append("\r
\r
");
string head = sb.ToString();
byte[] form_data = Encoding.UTF8.GetBytes(head);
//
byte[] foot_data = Encoding.UTF8.GetBytes("\r
--" + boundary + "--\r
");
//post
long length = form_data.Length + buffer.Length + foot_data.Length;
req.ContentLength = length;
Stream requestStream = req.GetRequestStream();
// , form_data > buffer > foot_data
//
requestStream.Write(form_data, 0, form_data.Length);
//
requestStream.Write(buffer, 0, buffer.Length);
//
requestStream.Write(foot_data, 0, foot_data.Length);
requestStream.Flush();
requestStream.Close();
//
WebResponse pos = req.GetResponse();
StreamReader sr = new StreamReader(pos.GetResponseStream(), Encoding.UTF8);
string result = sr.ReadToEnd().Trim();
sr.Close();
sr.Dispose();
if (pos != null)
{
pos.Close();
pos = null;
}
if (req != null)
{
req = null;
}
return result;
}
}
以上のコードの大部分はネットワークから来ており,出典は記載されていない.
処理手順は次のようになります.
(1)httpリクエストをHttpWebRequestでシミュレートする.
HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
(2)Web Responseで返信結果を受信する
WebResponse response = webRequest.GetResponse()