仅在httpwebrequest中的json字符串中传递值时获取错误

时间:2018-01-15 06:06:02

标签: c# json httpwebrequest

我正在执行HttpWebRequest来执行POST方法。我只有在传递值时才会出错,否则它会完美运行。

代码:

 public void CreateOrganization(string accountname)
    {
        var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
        http.Accept = "application/json";
        http.ContentType = "application/json";
        http.Method = "POST";
        string parsedContent = "{\"name\":\" "+accountname+" \"}";
        ASCIIEncoding encoding = new ASCIIEncoding();
        Byte[] bytes = encoding.GetBytes(parsedContent);
        Stream newStream = http.GetRequestStream();
        newStream.Write(bytes, 0, bytes.Length);
        newStream.Close();
        var response = http.GetResponse();
        var stream = response.GetResponseStream();
        var sr = new StreamReader(stream);
        var content = sr.ReadToEnd();
    }

它停止在Stream newStream = http.GetRequestStream(); 。

0 个答案:

没有答案