2012-07-03 280 views
2

我試圖通過C#發送HTTP請求。但它似乎不工作。這是我的工作。我想從您那裏得到修正,使其工作!通過ASP.NET C發送HTTP請求#

  String Mobile = txt_phone.Text; 
      String Message = "You have registered successfuly."; 
      Uri targetUri = new Uri("http://sms-om.com/smspro/sendsms.php?user=HatemSalem"); 
      System.Net.HttpWebRequest request = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(targetUri); 
+1

你什麼錯誤呢? –

+1

沒有錯誤。但是Http請求似乎不起作用。該代碼有什麼問題? – Aan

+1

那麼請求值中的數據是什麼?它可能不爲空... – Phil

回答

4

您目前還沒有執行的請求,使用:

var response = request.GetResponse() as HttpWebResponse; 

這是它實際上會從網上獲取數據,並給你任何培訓相關的錯誤

6

我沒有得到一個錯誤,而是一個「無效登錄」消息,這讓我覺得形式實際上使POST請求代替。這裏是一個Get Request示例:

using System; 
using System.Net; 
using System.Text; 
using System.IO; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     StringBuilder sb = new StringBuilder(); 

     byte[] buf = new byte[8192]; 

     //do get request 
     HttpWebRequest request = (HttpWebRequest) 
      WebRequest.Create("http://sms-om.com/smspro/sendsms.php?user=HatemSalem"); 


     HttpWebResponse response = (HttpWebResponse) 
      request.GetResponse(); 


     Stream resStream = response.GetResponseStream(); 

     string tempString = null; 
     int count = 0; 
     //read the data and print it 
     do 
     { 
      count = resStream.Read(buf, 0, buf.Length); 
      if (count != 0) 
      { 
       tempString = Encoding.ASCII.GetString(buf, 0, count); 

       sb.Append(tempString); 
      } 
     } 
     while (count > 0); 
     Response.Write(sb.ToString()); 
    } 
} 

祝你好運!

+0

如何將其轉換爲ASP.NET上使用? – Aan

+2

此代碼是Asp.Net。 –