2013-01-31 74 views
0

我有一個字符串,我想張貼到休息服務。發佈字符串休息服務

這怎麼能在C#中完成?一直googling一些,但還沒有找到一個解決方案的工作。

試過這一個:

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。似乎現在工作至少!

+0

什麼是您遇到問題?你有錯誤嗎?你的帖子失敗了嗎? –

+0

你遇到了什麼問題?什麼是失敗? –

+0

遠程服務器返回錯誤:(400)錯誤的請求。 – sindrem

回答

0

內容類型應爲文本/純文本