2009-10-27 25 views
1

我試圖做一個簡單的HTTP發佈帶有隻有url參數的端點。做一個HTTP POST到一個REST端點

至少,這是我如何理解以下說明:

POST到該地址有一個參數名爲url,改變了飼料的地址。

與XML-RPC方法一樣,它驗證Feed是否已更改,如果是,則通知訂閱者。

事件已記錄。返回值是一個名爲result的XML消息,具有兩個屬性success和msg。

這是目前我的代碼:

 public static void ping(string feed) 
    { 
     HttpWebResponse response = MakeRequest(feed); 
     XmlDocument document = new XmlDocument(); 

     document.Load(response.GetResponseStream(); 
     string success = document.GetElementById("success").InnerText; 
     string msg = document.GetElementById("msg").InnerText; 

     MessageBox.Show(msg, success); 
    } 
     private static HttpWebResponse MakeRequest(string postArgument) 
    { 
     string url = path + "?" + UrlEncode(postArgument); 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

     request.Method = "POST"; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     return (HttpWebResponse)request.GetResponse(); 
    } 
    private static string UrlEncode(string value) 
    { 
     string result; 
     result= HttpUtility.UrlEncode("url") + "=" + HttpUtility.UrlEncode(value); 
     return result; 
    } 

我從服務器獲取不正確的響應,所以我認爲我做錯了莫名其妙。這裏是響應:

在文檔的頂層無效。錯誤處理資源「文件:/// C:/Users/ADMIN/AppData/Local/Temp/VSD1.tmp.XML ...

街 ^

任何想法?

在此先感謝

+0

你從服務器得到什麼迴應? – 2009-10-27 23:40:46

回答

1

這裏是我找到工作的代碼。

我們使用HTTP POST,在流的主體中編碼參數。這給了我們「url-foobar」作爲內容主體。在內容中沒有內容類型,性格,邊界等。

 private static HttpWebResponse MakeRequest(string path, string postArgument) 
    { 
     //string url = path + "?" + UrlEncode(postArgument); 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path); 

     request.Method = "POST"; 
     string boundary = Guid.NewGuid().ToString().Replace("-", ""); 
     request.ContentType = "multipart/form-data; boundary=" + boundary; 
     Stream stream = request.GetRequestStream(); 
     string result = string.Format("url={0}", postArgument); 
     byte[] value = Encoding.UTF8.GetBytes(result); 
     stream.Write(value, 0, value.Length); 
     stream.Close(); 

     return (HttpWebResponse)request.GetResponse(); 
    } 
     public static void ping(string server, string feed) 
    { 
     HttpWebResponse response = MakeRequest(server, feed); 
     XmlDocument document = new XmlDocument(); 
     string result = GetString(response.GetResponseStream()); 
     try 
     { 
      document.LoadXml(result); 
     } 
     catch 
     { 
      MessageBox.Show("There was an error with the response", "Error"); 
     } 
     //MessageBox.Show(msg, success); 

    } 
    public static string GetString(Stream thestream) 
    { 
     int n = thestream.ReadByte(); 
     byte[] bytes = new byte[n]; 
     thestream.Read(bytes, 0, n); 
     return Encoding.ASCII.GetString(bytes); 
    } 

對GetString的調用僅用於調試目的,並非嚴格需要。

感謝大家誰插在這裏讓我走上正軌。

1

我不知道.NET的API,但我明白的說明爲:「執行與查詢參數的URL後」,和後體應該有url = foobar參數。

IOW:不是採用postArgument並將其追加到url,而是應該調用url並在消息正文中提供正確編碼的url = foobar。

此外:我沒有看到您設置請求「Accept」標頭,如果服務器使用它來識別響應的格式,這可能很重要。

1

在.NET中執行表單發佈的最佳方式是使用WebClient類。該類將以POST(實體主體中的參數)或GET(參數編碼爲查詢字符串參數)的方式發送數據。

而你需要向我們展示了實際的異常客戶端的回饋,爲了弄清楚什麼是錯的..

相關問題