如何发布数据到网站 [英] How to post data to a website

查看:164
本文介绍了如何发布数据到网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据发布到网站上。所以我创建了C#.net,我在所有控件打开这个网站,并填写(单选按钮,文本框,复选框等),从我的数据库中的值小的应用程序。我也有提交按钮点击事件。该应用程序,然后等待10至15秒,然后将文件从网页到我的数据库的响应。

I need to post data to a website. So I created a small app in C#.net where I open this website and fill in all the controls (radio buttons, text boxes, checkboxes etc) with the values from my database. I also have a click event on the SUBMIT button. The app then waits for 10-15 seconds and then copies the response from the webpage to my database.

正如你所看到的,这实在是一个忙碌的过程。如果有成千上万的记录上传,这个程序需要更长的时间(由于事实,即它会等待15秒的响应)。

As you can see, this is really a hectic process. If there are thousands of records to upload, this app takes much longer (due to fact that it waits 15s for the response).

是否有任何其他的方式来发布数据?我在寻找类似连接所有的字段有其价值,并上传它像数据流。 ?这将如何工作,如果该网站是HTTPS,而非HTTP

Is there any other way to post data? I am looking for something like concatenating all the fields with its value and uploading it like a stream of data. How will this work if the website is https and not http?

推荐答案

您可以使用的HttpWebRequest 要做到这一点,你可以连接所有要发布成请求一个字符串的值。它可能是这个样子:

You can use HttpWebRequest to do this, and you can concatenate all the values you want to post into a single string for the request. It could look something like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.yoursite.com");
request.Method = "POST";

formContent = "FormValue1=" + someValue +
    "&FormValue2=" + someValue2 +
    "&FormValue=" + someValue2;

byte[] byteArray = Encoding.UTF8.GetBytes(formContent);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = HttpUtility.UrlDecode(reader.ReadToEnd());
//You may need HttpUtility.HtmlDecode depending on the response

reader.Close();
dataStream.Close();
response.Close();

这方法应该工作正常HTTP和HTTPS。

This method should work fine for http and https.

这篇关于如何发布数据到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆