2011-09-09 63 views
0

我正在使用SMS發送和接收MVC3應用程序中的功能。我如何發送對URL的回覆?任何觸及我網頁的網址都會收到「確定」或「已收到」的回覆。如何發送對URL的響應?

例如,請考慮以下代碼,該代碼是從提供者發送到我的網站的。我需要發送一個響應文本,如「ok」或者接收到stringResult。如果我可以用一些「成功」參數來回應網址,那就太棒了。

string stringResult = null; 
stringpost ="parameters for url"; 
objWebRequest = (HttpWebRequest)WebRequest.Create("http://myip/app/action/receivesms?"); 
objWebRequest.Method = "POST" 
if ((objProxy1 != null)) 
{ 
    objWebRequest.Proxy = objProxy1; 
} 

objWebRequest.ContentType = "applicationwww-form-urlencoded"; 

objStreamWriter = new StreamWriter(objWebRequest.GetRequestStream()); 
objStreamWriter.Write(stringpost); 
objStreamWriter.Flush(); 
objStreamWriter.Close(); 

objWebResponse = (HttpWebResponse)objWebRequest.GetResponse(); 
objStreamReader = new StreamReader(objWebResponse.GetResponseStream()); 
stringResult = objStreamReader.ReadToEnd(); 
objStreamReader.Close(); 
return (stringResult); 
+0

您的'objWebRequest'方法引用了哪個類? –

回答

1

只是這樣做在你的控制器:

public ActionResult YourAction() 
{ 
    return Content("OK"); 
} 

或者,如果你想使用HTTP代碼,而不是字符串,你可以做類似:

public ActionResult YourAction() 
{ 
    // 204 is the HTTP code for OK with no content 
    return new HttpStatusCodeResult(204); 
} 
+0

謝謝Xavier 返回新的HttpStatusCodeResult(200);作爲resposne傳遞好。 – Rakesh

0

stringResult = objStreamReader.ReadToEnd(); 代碼中的stringResult接收服務器響應。這個迴應包含確定或succuessfully發送。 您必須解釋響應,然後將您的消息發送給客戶端,並且您可以使用ajax調用此send smsmethod。 在AJAX方法調用,您可以顯示「OK」或「收到」的對話框