Last active
July 21, 2016 14:59
-
-
Save fliedonion/1f08340da6cb0288c22fdda5b88892c3 to your computer and use it in GitHub Desktop.
C# Post With Binary Value Sample
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
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Windows.Forms; | |
namespace PostDataWithFlask { | |
public partial class Form1 : Form { | |
public Form1() { | |
InitializeComponent(); | |
} | |
private void button1_Click(object sender, EventArgs e) { | |
textBox1.Text = DoPost(); | |
} | |
private String DoPost(){ | |
var enc = Encoding.UTF8; | |
var postWordValue = "イ&ン=ター?ネッ%ト"; | |
// var postWordValue = "x=11&y=32?http://google.com/"; | |
if (postWordValue != Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(postWordValue))) { | |
postWordValue = "%" + BitConverter.ToString(enc.GetBytes(postWordValue)).Replace("-", "%"); | |
if (postWordValue == "%") postWordValue = ""; | |
} | |
else { | |
postWordValue = Uri.EscapeDataString(postWordValue); | |
} | |
//POST送信する文字列を作成 | |
string postData = "lang=" + Uri.EscapeDataString("ja=japanese") + "&word=" + postWordValue; | |
byte[] postDataBytes = Encoding.ASCII.GetBytes(postData); | |
//WebRequestの作成 | |
var req = (HttpWebRequest)WebRequest.Create("http://localhost:5000/"); | |
//メソッドにPOSTを指定 | |
req.Method = "POST"; | |
req.ContentType = "application/x-www-form-urlencoded"; | |
req.ContentLength = postDataBytes.Length; | |
//データをPOST送信するためのStreamを取得 | |
using (var reqStream = req.GetRequestStream()) { | |
reqStream.Write(postDataBytes, 0, postDataBytes.Length); | |
} | |
var returnValue = ""; | |
//サーバーからの応答を受信するためのWebResponseを取得 | |
var res = req.GetResponse() as HttpWebResponse; | |
using(var rs = res.GetResponseStream()) | |
using (var sr = new System.IO.StreamReader(rs, enc)) { | |
returnValue = sr.ReadToEnd(); | |
} | |
return returnValue; | |
} | |
} | |
} |
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
# coding: utf-8 | |
from flask import Flask | |
from flask import request | |
import binascii | |
app = Flask(__name__) | |
@app.route('/', methods=['GET','POST']) | |
def hello(): | |
#return binascii.hexlify(request.stream.read()) | |
return request.form['lang'] + " " + request.form['word'] | |
if __name__ =='__main__': | |
app.run() | |
## setup sample | |
# > mkdir c:\temp | |
# > cd c:\temp | |
# > mkdir flask-get-post && cd flask-get-post | |
# > virtualenv venv | |
# > venv\scripts\activate | |
# > pip install flask |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment