c#更改此行,使其将字符串作为变量输出
更新到POST在这段代码之后的原始帖子---这段代码是对david一直在帮助我做的事情的更新,抛出一个错误需要帮助c#更改此行,使其将字符串作为变量输出,c#,linq,variables,postdata,C#,Linq,Variables,Postdata,更新到POST在这段代码之后的原始帖子---这段代码是对david一直在帮助我做的事情的更新,抛出一个错误需要帮助 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.Specialized; using System.Net; using System.IO; namespace ConsoleApplication1 {
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
如何更改它,使其将字符串输出到变量(如“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);
}
}
}
那么,如何将机器名脚本添加到此函数,然后将其用作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;
}
}
}
为什么它需要在单独的应用程序中?如果一个应用程序的输出将成为另一个应用程序输入的命令行参数,那么它们都在同一台机器上运行。这意味着,在本例中,它们都从
System.Environment.MachineName
获得相同的值
您只需在需要的应用程序中获取值:
formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);
为什么它必须在单独的应用程序中?你就不能这样做:
formData[“var1”]=string.Format(“MachineName:{0}”,System.Environment.MachineName)代码>?写下它作为我的答案,我会勾选它,因为这是我一直在寻找的ifo,我的脚本现在可以运行了:)davind你能帮我将另一个控制台应用程序集成到另一个formdat变量(如last)中吗time@user2847609:嗯,如果你有关于堆栈溢出的其他问题,那么我也许可以看一看。这与这个问题有关真的吗?我会添加其他源代码,你可以在这里再次添加答案,我会勾选它。我会立即更新我的代码。它已更新。如果你能帮助我将此脚本包含到我在c#中的其他控制台应用程序中,我会很高兴将结果输出为var2@user2847609:关于您的编辑。。。听起来你想开始做的是创建函数来计算你的值。因此,您可以创建一个单独的函数来返回结果,而不是将结果写入控制台的单独应用程序。类似于private string CalculateSomeValue(){//code here}
的东西,然后您可以使用以下命令调用它:formData[“var1”]=CalculateSomeValue()代码>
formData["var1"] = formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);
formData["var2"] = "this is where this script needs to be ";
formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);