var request = (HttpWebRequest)WebRequest.Create("https://api.example.tv/customers/4/profiles");
Console.WriteLine (Jsondata + "This is Json");
var postData = "first_name=hello";
postData += "&last_name=world";
postData += "&pin=1234";
var data = Encoding.ASCII.GetBytes(postData);
Console.WriteLine (data + "Some Crap Data");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
我有Json数据,但我想转换为urlencoded现在我正在以上述方式进行
Person p = new Person();
p.Name = "Apple";
string json = JsonConvert.SerializeObject(p);
但现在我想把它转换为url编码有没有正确的方法呢?
答案 0 :(得分:0)
试试这个:
Person p = new Person();
p.Name = "Apple";
var urlEncodedString = String.Join("&", p.GetType().GetProperties()
.Select(property => property.Name + "=" + HttpUtility.UrlEncode(property.GetValue(p, null).ToString()))
.ToArray());