2013-10-06 65 views
0

更新到POST ORIGIANAL POST此代碼後---這個代碼是更新一下大衛一直在幫助我做了一個扔錯誤需要幫助C#更改此行,它輸出的字符串作爲變量

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Collections.Specialized; 
using System.Net; 
using System.IO; 


namespace ConsoleApplication1 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 
     string URL = "http://localhost/test2.php"; 
     WebClient webClient = new WebClient(); 

     NameValueCollection formData = new NameValueCollection(); 
     formData["var1"] = formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName); 
     formData["var2"] = ip(); 


     byte[] responseBytes = webClient.UploadValues(URL, "POST", formData); 
     string responsefromserver = Encoding.UTF8.GetString(responseBytes); 
     Console.WriteLine(responsefromserver); 
     webClient.Dispose(); 
     System.Threading.Thread.Sleep(5000); 
    } 
    public void ip() 
    { 
     String publicIP = ""; 

     System.Net.WebRequest request = System.Net.WebRequest.Create("http://checkip.dyndns.org/"); 
     using (System.Net.WebResponse response = request.GetResponse()) 
     { 
      using (System.IO.StreamReader stream = new System.IO.StreamReader(response.GetResponseStream())) 
      { 
       publicIP = stream.ReadToEnd(); 
      } 
     } 

     //Search for the ip in the html 
     int first = publicIP.IndexOf("Address: ") + 9; 
     int last = publicIP.LastIndexOf("</body>"); 
     publicIP = publicIP.Substring(first, last - first); 

     Console.WriteLine(publicIP); 
     System.Threading.Thread.Sleep(5000); 


    } 
} 
} 

this is the error I am getting 
Error 2 - An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Program.ip()' 
I am trying to create a function that will send the out put as var2 

我有這樣的C#腳本

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace ConsoleApplication4 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     Console.WriteLine("MachineName: {0}", System.Environment.MachineName); 
     System.Threading.Thread.Sleep(5000); 
    } 
} 
} 

我怎樣才能改變這種做法,它輸出字符串變量說「VAR2」,並在此腳本中使用它

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Collections.Specialized; 
using System.Net; 
using System.IO; 


namespace ConsoleApplication1 
{ 
class Program 
{ 

    static void Main(string[] args) 
    { 
     string URL = "http://localhost/test2.php"; 
     WebClient webClient = new WebClient(); 
     NameValueCollection formData = new NameValueCollection(); 
     formData["var1"] = "THIS IS WHERE VAR2 NEEDS TO BE "; 


     byte[] responseBytes = webClient.UploadValues(URL, "POST", formData); 
     string responsefromserver = Encoding.UTF8.GetString(responseBytes); 
     Console.WriteLine(responsefromserver); 
     webClient.Dispose(); 
     System.Threading.Thread.Sleep(5000); 
    } 
} 
} 

所以,我怎麼可以添加機器名SCRIPT給這個函數,然後用它作爲VAR2 任何幫助將是輝煌的

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace ConsoleApplication5 
{ 
class Program 
{ 
    public static int Main(string[] args) 
    { 
     String publicIP = ""; 

     System.Net.WebRequest request = System.Net.WebRequest.Create("http://checkip.dyndns.org/"); 
     using (System.Net.WebResponse response = request.GetResponse()) 
     { 
      using (System.IO.StreamReader stream = new System.IO.StreamReader(response.GetResponseStream())) 
      { 
       publicIP = stream.ReadToEnd(); 
      } 
     } 

     //Search for the ip in the html 
     int first = publicIP.IndexOf("Address: ") + 9; 
     int last = publicIP.LastIndexOf("</body>"); 
     publicIP = publicIP.Substring(first, last - first); 

     Console.WriteLine(publicIP); 
     System.Threading.Thread.Sleep(5000); 
     return 0; 
    } 
} 
} 

她被更新大衛,我想在我的其他腳本incude這個腳本所以它看起來像這樣

formData["var1"] = formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName); 
formData["var2"] = "this is where this script needs to be "; 
+0

爲什麼一定要在一個單獨的應用程序?你不能這樣做:'formData [「var1」] = string.Format(「MachineName:{0}」,System.Environment.MachineName);'? – David

+0

寫道,作爲我的答案,我會打勾它,因爲這是ifo我正在尋找我的腳本現在功能:) – user2847609

回答

1

爲什麼它需要在一個單獨的應用程序?如果一個應用程序的輸出將成爲另一個應用程序輸入的命令行參數,那麼它們都運行在同一臺計算機上。這意味着,在這種情況下,他們都會從System.Environment.MachineName獲得相同的值。

你可以得到需要的地方在應用中值:

formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName); 
+0

davind你也可以幫我把另一個控制檯應用程序整合到另一個formdat var上次 – user2847609

+0

@ user2847609:好,如果你有關於堆棧溢出的另一個問題,那麼我可以看看這個。 – David

+0

它與這個問題真的生病添加其他源代碼,你總是可以添加答案在這裏再次和病蜱它不適當更新我的代碼現在 – user2847609