有誰知道 在 C# MVC Controller 做 JSON POST 並且導向 (預設是不行)
NUGET 套件也行
對下面這個網址 POST 下面 JSON 字串
http://localhost:9300/bi/v1/login
{"parameters":[{"name":"h_CAM_action","value":"logonAs"},{"name":"CAMNamespace","value":"JDBCAuth"},{"name":"CAMUsername","value":"ABCD"},{"name":"CAMPassword","value":"ABCD"}]}
MVC一樣適用,我用的是nuget下載的newtonsoft.json
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
namespace MVC_A
{
class Program
{
static void Main(string[] args)
{
myobjclass myObj = new myobjclass(new List<parameter>{
new parameter("h_CAM_action", "logonAs"),
new parameter("CAMNamespace", "JDBCAuth"),
new parameter("CAMUsername", "ABCD"),
new parameter("CAMPassword", "ABCD"),
});
Console.WriteLine(JsonConvert.SerializeObject(myObj));
Console.ReadKey();// 等你按任一鍵
POST(new Uri("https://test.abcd.com/a.php"),myObj);
}
private static void POST(Uri servSite, myobjclass myObj)
{
using (WebClient wc = new WebClient())
{
try
{
wc.Encoding = Encoding.UTF8;
var res = wc.UploadString(servSite,JsonConvert.SerializeObject(myObj) );
Console.WriteLine(res);
}
catch (WebException ex)
{
throw new Exception("無法連接遠端伺服器");
}
}
}
}
class myobjclass
{
public List<parameter> parameters { get; set; }
public myobjclass(List<parameter> initValue)
{
parameters = new List<parameter>();
if (initValue.Any())
parameters.AddRange(initValue);
}
}
class parameter
{
public string name { get; set; }
public string value { get; set; }
public parameter(string _name, string _value)
{
name = _name;
value = _value;
}
}
}