0

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

这如何在 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。似乎至少现在可以工作!

4

1 回答 1

0

Content-Type 应该是 text/plain

于 2013-10-04T21:46:49.000 回答