【发布时间】:2013-10-11 23:16:07
【问题描述】:
好的,我已经创建了一个 c# 控制台源代码,但它不能按我的意愿工作。
我需要将数据发布到一个 URL,就像我要在浏览器中输入一样。
url with data = localhost/test.php?DGURL=DGURL&DGUSER=DGUSER&DGPASS=DGPASS
这是我的 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/test.php";
WebClient webClient = new WebClient();
NameValueCollection formData = new NameValueCollection();
formData["DGURL"] = "DGURL";
formData["DGUSER"] = "DGUSER";
formData["DGPASS"] = "DGPASS";
byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
string responsefromserver = Encoding.UTF8.GetString(responseBytes);
Console.WriteLine(responsefromserver);
webClient.Dispose();
}
}
}
我还在 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 URI = "http://localhost/test.php";
string myParameters = "DGURL=value1&DGUSER=value2&DGPASS=value3";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "text/html";
string HtmlResult = wc.UploadString(URI, myParameters);
System.Threading.Thread.Sleep(500000000);
}
}
}
}
几天来,我一直在尝试在我的 c# 控制台中找到一种方法来做到这一点
【问题讨论】:
-
有什么问题?会发生什么?
-
我有一个 php 脚本等待 url 发布,然后如果我手动访问数据添加的 url,则将数据添加到 MySQL,但运行此代码时它不会添加
-
我误打了帽子,这里没有大喊大叫
-
我们喜欢大喊大叫。但说真的,您在顶部的示例 url 使用查询字符串,然后您将值作为 post 参数传递,您想要哪种方式?
-
首先,您使用的是 post 方法,其中 url 内部是一个 get 方法,只需将其切换为 get。