将字符串发布到休息服务

时间:2013-01-31 14:23:27

标签: c#

我有一个字符串,我想发布到休息服务。

如何在C#中完成?谷歌搜索了一些,但没有找到一个有效的解决方案。

试过这个:

byte[] paramBytes = Encoding.UTF8.GetBytes(xml);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(
    "https://secure.mm.staging.boostcom.net/connect/rest/groups/1981891989/tokens");
req.Method = "POST";
req.ContentLength = paramBytes.Length;
req.ContentType = "text/xml";
ServicePointManager.ServerCertificateValidationCallback = 
    delegate(
        object s, 
        X509Certificate certificate, 
        X509Chain chain, 
        SslPolicyErrors sslPolicyErrors) 
        { 
            return true; 
        };

using (Stream reqStream = req.GetRequestStream())
{
    reqStream.Write(paramBytes, 0, paramBytes.Length);
}

using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) //HERE!
{
    if (resp.StatusCode != HttpStatusCode.OK)
    {
        string message = String.Format(
            "POST failed. Received HTTP {0}", 
             resp.StatusCode);
        throw new ApplicationException(message);
    }

    StreamReader sr = new StreamReader(resp.GetResponseStream());
    string response = sr.ReadToEnd();

    Console.WriteLine(response + System.Environment.NewLine);
}

解决方案:我可能有错误的contentType。似乎现在至少工作!

1 个答案:

答案 0 :(得分:0)

Content-Type应为text / plain