2010-01-13 51 views
0

我必須將一些信息傳遞給weburl並從中得到響應。通過基於桌面的應用程序的URL發送信息

這必須在c#中使用基於桌面的應用程序的按鈕單擊完成。

+0

爲什麼這不是一個真正的問題?這有點簡單,但它肯定是一個有效的問題。 – Josh 2010-01-13 05:57:51

回答

3
StringBuilder sb = new StringBuilder(); 

// used on each read operation 
byte[]  buf = new byte[8192]; 

// prepare the web page we will be asking for 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.feefifofum.com/login.aspx?userid=XXX&pass=YYYY"); 

// execute the request 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
// we will read data via the response stream 
Stream resStream = response.GetResponseStream(); 

string tempString = null; 
int count  = 0; 
do 
{ 
    // fill the buffer with data 
    count = resStream.Read(buf, 0, buf.Length); 

    // make sure we read some data 
    if (count != 0) 
    { 
     // translate from bytes to ASCII text 
     tempString = Encoding.ASCII.GetString(buf, 0, count); 

     // continue building the string 
     sb.Append(tempString); 
    } 
} 
while (count > 0); // any more data to read? 

// print out page source 
Console.WriteLine(sb.ToString()); 
1

使用WebClient.DownloadString並通過您的值作爲查詢字符串。像下面

 
string s; 
using (WebClient wc = new WebClient()) 
{ 
    wc.QueryString.Add ("Param1", "param1value"); 
    wc.QueryString.Add ("Param2", "param2value");   
    s = wc.DownloadString (webaddress); 
} 

對於方法詳情,請訪問MSDN

1

信息發送到一個URL和獲取信息傳回東西也聽起來有點像一個web服務的工作。你有沒有考慮過使用Web服務? (假設你有控制/訪問URL)

相關問題