C#Smarty Streets POST请求

时间:2015-02-08 21:28:11

标签: c# post smartystreets

我正在尝试向Smarty Streets API发送POST,但我收到的响应是一个格式错误的有效负载。我浏览了网站上的文档,我认为我已经正确地发送了所有内容。我甚至用Fiddler来看看我发送的东西,它看起来是正确的,但肯定有一些我不知道的东西。我是比较新的编程。所以任何帮助都会受到赞赏。

string url = Uri.EscapeUriString("http://api.smartystreets.com/street-address?auth-id=Id&auth-token=token");
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

TestAddress json = new TestAddress();

json.street = "11 Phelan Ave, San Francisco, CA";

string jsoncvt = JsonConvert.SerializeObject(json);
byte[] byteArray = Encoding.UTF8.GetBytes(jsoncvt);
httpWebRequest.ContentLength = byteArray.Length;
httpWebRequest.Host = "api.smartystreets.com";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept= "application/json";
httpWebRequest.Method = "POST";
Stream dataStream = httpWebRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

1 个答案:

答案 0 :(得分:0)

正如Jonathan Oliver评论的那样,SmartyStreets API预计会有一系列输入候选者。只需将序列化有效负载修改为集合:

string jsoncvt = JsonConvert.SerializeObject(new TestAddress[] {json});