如何使用该POST方法发出HTTP请求并发送一些数据?我可以做GET请求,但不知道如何制作POST.
我有一个除了一些字符串之外什么都没有的网页.没有图像,没有背景颜色或任何东西,只是一些长度不是很长的纯文本.
我只是想知道,什么是最好的(通过这种方式,我的意思是最快和最有效)的方式来传递网页中的字符串,以便我可以用它来做其他事情(例如在文本框中显示)?我知道WebClient,但我不确定它是否会做我想做的事情,而且即使它确实有效,我也不想尝试它,因为我上次做的时间大约需要30秒一个简单的操作.
任何想法,将不胜感激.
我使用WebClient从网站下载一个字符串(只包含纯文本,没有别的),所以我使用DownloadString方法:
WebClient wc = new WebClient();
string str = wc.DownloadString("http://blah");
Run Code Online (Sandbox Code Playgroud)
它运行正常,但问题是它第一次下载字符串需要很长时间,比如5秒.之后它运作得很快.为什么会发生这种情况,如何解决这个问题呢?
我在我的Form构造函数中,在InitializeComponent之后有以下代码:
using (WebClient client = new WebClient())
{
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
client.DownloadDataAsync("http://example.com/version.txt");
}
Run Code Online (Sandbox Code Playgroud)
当我启动表单时,UI不会出现,直到引发client_DownloadDataCompleted.client_DownloadDataCompleted方法为空,因此没有问题.
我做错了什么?如何在不冻结UI的情况下做到这一点?
谢谢你的时间.
最好的祝福.
完整代码:
Program.cs中
using System;
using System.Windows.Forms;
namespace Lala
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Run Code Online (Sandbox Code Playgroud)
Form1.cs的
using System;
using System.Net;
using System.Windows.Forms;
namespace Lala
{
public partial class Form1 : Form
{
WebClient client = new WebClient();
public Form1()
{ …Run Code Online (Sandbox Code Playgroud)