いつもお世話になります。
C#でPOSTしてデータを取得したいです。
環境は、Visual Studio 2013 Express edition Windows 8.1です。
Android/Javaの環境のコードは次のとおりです。
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private JsonReturnTest jsonReturnTest; private OnFinishListener ofl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); JSONObject json = new JSONObject(); try { json.put("method","get_flg"); json.put("mission_code",2); json.put("route_code",1); json.put("flg_no",1); } catch (JSONException e) { e.printStackTrace(); } jsonReturnTest = new JsonReturnTest(this); jsonReturnTest.post("http://192.168.0.1/Api/method/", json, ofl); } private void setVolleyEvent(){ OnFinishListener ofl = new OnFinishListener(){ //通信後のデータ受け取り @Override public void onFinish( JSONObject o ){ //受け取った情報をToastに表示 showToast(o); } }; } //受け取った情報をToastに表示 private void showToast( JSONObject o ){ Toast.makeText(this, o.toString(), Toast.LENGTH_LONG).show(); }
}
これと同様になるように、C#でコードを書きました。
参照設定で、
System.Net
System.Net.Http
System.Runtime.Serialization
System.Web
を追加しています。
サーバーからのレスポンスはOKと返ってきています。
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Date: Wed, 25 Nov 2015 01:30:12 GMT
Content-Length: 48
Content-Language: ja
Content-Type: application/json
}
OK
と出ます。
気になるのは、送信contentsが空であることです。
jsonのオブジェクトをうまく作れていないように思います。
アドバイスお願いします。
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JSON {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
string URL = "http://192.168.0.1/Api/method/";
private void Form1_Load(object sender, EventArgs e) { var response = Command(); } private async Task<string> Command(){ try{ // POST 先のURL Uri theUri = new Uri(URL); //HttpClient を作成しヘッダーを設定する HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Host = theUri.Host; // JSON としてシリアライズし POST するデータ JSONObject json = new JSONObject(); //値をセットします json.method = "get_flg"; json.mission_code = 2; json.route_code = 1; json.flg_no = 1; // 該当タイプ用の JsonSerializer を作成 DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(JSONObject)); // Serializer を使ってオブジェクトを MemoryStream に書き込み MemoryStream meorystream = new MemoryStream(); jsonSerializer.WriteObject(meorystream, json); meorystream.Position = 0; // StreamReader で StringContent (Json) をコンストラクトします。 StreamReader streamreader = new StreamReader(meorystream); StringContent content = new StringContent(streamreader.ReadToEnd(), System.Text.Encoding.UTF8, "application/json"); string 送信contents = streamreader.ReadToEnd(); MessageBox.Show(送信contents);//これが空。 // データを送信する。 HttpResponseMessage responseMessage = await client.PostAsync(theUri, content); String failureMessage; if (responseMessage.IsSuccessStatusCode) { MessageBox.Show(responseMessage.ToString()); } else { // 応答ステータスコードを表示する。 failureMessage = "HTTP Status: " + responseMessage.StatusCode.ToString() + " - Reason: " + responseMessage.ReasonPhrase; MessageBox.Show(failureMessage); } } catch (Exception error){ MessageBox.Show(error.Message); } finally { MessageBox.Show("正常に終わったみたいです"); } return ("1"); }
}
[DataContract] public class JSONObject { public int Id { get; set; } [DataMember(Name = "method")] public string method { get; set; } [DataMember(Name = "mission_code")] public int mission_code { get; set; } [DataMember(Name = "route_code")] public int route_code { get; set; } [DataMember(Name = "flg_no")] public int flg_no { get; set; } }
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。