通过webapi获取jsonp数据

时间:2016-12-27 07:57:30

标签: c#

我使用HttpRequest对象从webapi中获取数据,这些数据可以放置Jsonp数据。但是,尽管Chrome网络显示确实将数据放回去,但responseFromServer对象是空的。这是我的代码(c#)。非常感谢。

    WebRequest request = WebRequest.Create(url);
    request.Method = "post";

    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    request.ContentType = "application/json";
    request.ContentLength = byteArray.Length;

    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    WebResponse response = request.GetResponse();
    var status = ((HttpWebResponse)response).StatusDescription;

    dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();

0 个答案:

没有答案