2010-09-16 26 views
2

我需要打開系統默認瀏覽器併發送到一些自定義URI POST數據。所以我有兩部分代碼:第一個 - 打開def瀏覽器,另一個必須發送POST數據,但不這樣做。在默認瀏覽器中發送POST請求到自定義URI

你能說什麼呢?

enter code here private void button1_Click(object sender, EventArgs e) 
    { 

     string browser = string.Empty; 
     RegistryKey key = null; 
     try 
     { 
      key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false); 

      //trim off quotes 
      browser = key.GetValue(null).ToString().ToLower().Replace("\", ""); 
      if (!browser.EndsWith("exe")) 
      { 
       //get rid of everything after the ".exe" 
       browser = browser.Substring(0, browser.LastIndexOf(".exe")+4); 
      } 
     } 
     finally 
     { 
      if (key != null) key.Close(); 
     } 
     //open default system browser 
     System.Diagnostics.Process.Start(browser, strURL.Text); 

// ***************************************** **********

 // Convert string data into byte array 
     string strData = "Name=Sergiy&Age=21"; 
     byte[] dataByte = Encoding.UTF8.GetBytes(strData); 

     HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create(strURL.Text); 
     POSTRequest.Method = "POST"; 
     // Set the content type - Mine was xml. 
     POSTRequest.ContentType = "application/x-www-form-urlencoded"; 
     POSTRequest.KeepAlive = false; 
     POSTRequest.Timeout = 5000; 
     POSTRequest.ContentLength = dataByte.Length; 
     // Get the request stream 
     Stream POSTstream = POSTRequest.GetRequestStream(); 
     // Write the data bytes in the request stream 
     POSTstream.Write(dataByte, 0, dataByte.Length); 

     //Get response from server 
     HttpWebResponse POSTResponse = (HttpWebResponse)POSTRequest.GetResponse(); 

    } 

回答

1

我認爲你對瀏覽器的唯一控制是它打開的URL。您可能可以將它傳遞給一個文件:// some/path URL,其中包含要作爲JavaScript運行的代碼。瀏覽器將啓動,轉到文件://您指定的文件,在文件中運行javascript,然後文章的結果將顯示在瀏覽器中。