2010-11-11 54 views
2

我是新來的寧靜服務,我一直在創建一系列簡單的控制檯應用程序以更好地理解。我有一個simlple服務,我試圖發送數據,但我不斷收到400錯誤的請求錯誤。我知道它必須是我忽略的簡單東西。任何幫助將不勝感激。由於幫助簡單寧靜的帖子問題

//service contract 
[OperationContract, WebInvoke(Method = "POST", UriTemplate = "Test")] 
bool Test(string input); 

//service 
public bool Test(string input) 
{ 
    Console.Out.WriteLine("recieved [" + input + "]"); 
    return true; 
} 

//host program 
class Program 
{ 
    static void Main(string[] args) 
    { 
     Uri baseAddress = new Uri("http://localhost:8889/TestImage"); 
     WebServiceHost host = new WebServiceHost(typeof(ImageTestService), baseAddress); 

     try 
     { 
      host.Open(); 

      Console.Out.WriteLine("TestService hosted at {0}", baseAddress.ToString()); 
      Console.Out.WriteLine("hit enter to terminate"); 
      Console.In.ReadLine(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
      Console.ReadKey(); 
     } 
     finally 
     { 
      if (host.State == CommunicationState.Faulted) 
       host.Abort(); 
      else 
       host.Close(); 
     } 
    } 
} 

//client program 
// Create the web request 
Uri address = new Uri("http://localhost:8889/TestImage/Test"); 
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; 

// Set type to POST 
request.Method = "POST"; 
request.ContentType = "application/x-www-form-urlencoded"; 

StringBuilder data = new StringBuilder(); 
data.Append("input=" + HttpUtility.UrlEncode("12345")); 

// Create a byte array of the data we want to send 
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); 

// Set the content length in the request headers 
request.ContentLength = byteData.Length; 

// Write data 
using (Stream postStream = request.GetRequestStream()) 
{ 
    postStream.Write(byteData, 0, byteData.Length); 
    postStream.Close(); 
} 

// Get response 
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
{ 
    // Get the response stream 
    StreamReader reader = new StreamReader(response.GetResponseStream()); 
} 

回答

0

不知道這是回答你的問題 - 但我有我一直在使用表單提交的擴展方法:

public static HttpWebResponse DoFormPost(this HttpWebRequest request, string postVals, int timeoutSeconds) 
    { 
     request.Method = "POST"; 
     request.Timeout = timeoutSeconds * 0x3e8; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.AllowAutoRedirect = false; 
     byte[] bytes = Encoding.UTF8.GetBytes(postVals); 
     request.ContentLength = bytes.Length; 
     using (Stream stream = request.GetRequestStream()) 
     { 
      stream.Write(bytes, 0, bytes.Length); 
     } 
     return (HttpWebResponse)request.GetResponse(); 
    } 

或者因爲你加了標籤WCF,那麼還有另一個類似的問題:

Getting an http 400 error on calling a restful wcf service using http post

0

不幸的是,我不知道什麼是你的代碼錯誤,它看起來還好乍一看。我想知道它是否是您使用的UriTemplate。如果你的方法是「測試」和UriTemplate是「測試」,你可能需要使用該URL來調用它(兩個「測試」字符串):

Uri address = new Uri("http://localhost:8889/TestImage/Test/Test"); 

我相信方法名稱爲URL的一部分通過默認,所以模板在此之後被應用。

有了HTTP錯誤,我使用了一個名爲Fiddler的工具來排除故障。如果您可以創建與Request Builder一起工作的請求,那麼只需要確定如何通過代碼生成該請求即可。

+0

感謝您response.I想你的建議,加上另一個「測試」的網址,但我得到了404。我安裝了Fiddler,我可以看到我發送給該服務的內容,但對我來說這一切都很好。如果服務器方法有一個名爲「input」的參數,那麼我是否必須在後綴字符串中包含parm naem,或者只有在URL模板中的parm nameis時才這樣做? – user503926 2010-11-11 23:58:56

0

您正在調用的WCF服務期望您傳遞的字符串在XML中被序列化!

以下爲我工作在過去的:

string body = "foo"; 
string postData = @"<string xmlns='http://schemas.microsoft.com/2003/10/Serialization/'><![CDATA[" + body + "]]></string>";