我希望有人能帮助我一段令我困惑的问题。是否可以将数据从C#应用程序发送到网站/网络服务器?
是否有可能在打开浏览器时将数据从C#应用程序发送到网页,例如:如果你有一个在线登录表单,并想从你的C#应用程序发布一个用户名和密码到该表单。
我看到像打开的浏览器的页面形式和程序发布登录信息到该页面后,页面可以处理的形式马上从而在登录并将其发送到首页。这可能吗?
在此先感谢
保罗
我希望有人能帮助我一段令我困惑的问题。是否可以将数据从C#应用程序发送到网站/网络服务器?
是否有可能在打开浏览器时将数据从C#应用程序发送到网页,例如:如果你有一个在线登录表单,并想从你的C#应用程序发布一个用户名和密码到该表单。
我看到像打开的浏览器的页面形式和程序发布登录信息到该页面后,页面可以处理的形式马上从而在登录并将其发送到首页。这可能吗?
在此先感谢
保罗
肯定。
您可以使用HttpWebRequest与网页或Web服务器(或Web服务)进行通信。这是实际的代码,我用它来从Facebook网址获取数据:
internal static string FbFetch(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseText = reader.ReadToEnd();
return responseText;
}
}
}
不过,你描述的被称为“HTML屏幕抓取”什么,它可以用于构建应用程序的一个繁琐而脆的方法。单调乏味,因为很难浏览所有的UI糖果,而且很脆弱,因为如果页面设计师改变他的页面,你的屏幕抓取将不再起作用。
祝你好运。
处理,非常简洁。我打算建议使用'WebClient',但这足够短,确实没有意义。 – 2010-12-17 21:26:18
谢谢,看起来很有用,但我需要打开用户默认的网页浏览器,这是否可行? – AdrenalineJunky 2010-12-17 21:32:55
另外,这是使用GET,有没有相当的职位? – AdrenalineJunky 2010-12-17 21:34:04
要回答你关于HTTP帖子(附件)的问题,它更难 - 因为附件。这是我用来将图像发布到FaceBook的实际代码。
/// <summary>
/// Create a new HttpWebRequest with the default properties for HTTP POSTS
/// </summary>
/// <param name="url">The URL to be posted to</param>
/// <param name="referer">The refer</param>
/// <param name="cookies">CookieContainer that should be used in this request</param>
/// <param name="postData">The post data</param>
private string CreateHttpWebUploadRequest(string url, string referer, CookieContainer cookies, NameValueCollection postData, FileInfo fileData, string fileContentType)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
string boundary = "----------" + DateTime.UtcNow.Ticks.ToString("x", CultureInfo.InvariantCulture);
// set the request variables
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.CookieContainer = cookies;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.55 Safari/533.4";
request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, */*";
request.Headers.Add("Accept-Encoding: gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.Headers.Add("Accept-Language: en-us");
request.Referer = referer;
request.KeepAlive = true;
request.AllowAutoRedirect = false;
// process through the fields
StringBuilder sbHeader = new StringBuilder();
// add form fields, if any
if (postData != null)
{
foreach (string key in postData.AllKeys)
{
string[] values = postData.GetValues(key);
if (values != null)
{
foreach (string value in values)
{
if (!string.IsNullOrEmpty(value))
sbHeader.AppendFormat("--{0}\r\n", boundary);
sbHeader.AppendFormat("Content-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}\r\n", key, value);
}
}
}
}
if (fileData != null)
{
sbHeader.AppendFormat("--{0}\r\n", boundary);
sbHeader.AppendFormat("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n", "media", fileData.Name);
sbHeader.AppendFormat("Content-Type: {0}\r\n\r\n", fileContentType);
}
byte[] header = Encoding.UTF8.GetBytes(sbHeader.ToString());
byte[] footer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
long contentLength = header.Length + (fileData != null ? fileData.Length : 0) + footer.Length;
// set content length
request.ContentLength = contentLength;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(header, 0, header.Length);
// write the uploaded file
if (fileData != null)
{
// write the file data, if any
byte[] buffer = new Byte[fileData.Length];
var bytesRead = fileData.OpenRead().Read(buffer, 0, (int)(fileData.Length));
requestStream.Write(buffer, 0, bytesRead);
}
// write footer
requestStream.Write(footer, 0, footer.Length);
requestStream.Flush();
requestStream.Close();
using (var response = request.GetResponse() as HttpWebResponse)
{
using (var stIn = new System.IO.StreamReader(response.GetResponseStream()))
{
return stIn.ReadToEnd();
}
}
}
}
更新 并使其完整,这里是不需要的文件附件职位的代码。再次,我使用此代码发布到FaceBook。
/// <summary>
/// Create a new HttpWebRequest with the default properties for HTTP POSTS
/// </summary>
/// <param name="url">The URL to be posted to</param>
/// <param name="referer">The refer</param>
/// <param name="cookies">CookieContainer that should be used in this request</param>
/// <param name="postData">The post data (needs to be formatted in name=value& format</param>
private string CreateHttpWebPostRequest(string url, string referer, CookieContainer cookies, NameValueCollection postData)
{
var sbPostData = new StringBuilder();
if (postData != null)
{
foreach (string key in postData.AllKeys)
{
string[] values = postData.GetValues(key);
if (values != null)
{
foreach (string value in values)
{
if (!string.IsNullOrEmpty(value))
sbPostData.Append(string.Format("{0}={1}&", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value)));
}
}
}
}
var parameterString = Encoding.UTF8.GetBytes(sbPostData.ToString());
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = WebRequestMethods.Http.Post;
request.CookieContainer = cookies;
request.ContentLength = parameterString.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.55 Safari/533.4";
request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, */*";
request.Headers.Add("Accept-Encoding: gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.Headers.Add("Accept-Language: en-us");
request.Referer = referer;
request.KeepAlive = true;
request.AllowAutoRedirect = false;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(parameterString, 0, parameterString.Length);
requestStream.Close();
using (var response = request.GetResponse() as HttpWebResponse)
{
using (var stIn = new System.IO.StreamReader(response.GetResponseStream()))
{
return stIn.ReadToEnd();
}
}
}
}
嘿,这不是那种类似于C#facebooksdk http://facebooksdk.codeplex中的代码的东西。com – 2010-12-19 04:14:53
谢谢杰森,我试过的代码,但似乎无法使其工作在https登录,是否有任何区别发布到https而不是http?或者,问题可能是Cookie没有正确创建,或者它没有正确重定向,某些事情绝对不是奇怪的工作/行为。有任何想法吗? – AdrenalineJunky 2010-12-19 20:03:32
@Shekhar_Pro - 不,我创建了这个专门与Facebook Graph API进行通信。不使用facebooksdk。我确实从http://skysanders.net/subtext/archive/2010/04/12/c-file-upload-with-form-fields-cookies-and-headers.aspx得到了帮助,但 – 2010-12-20 01:49:17
您可以直接在C#中自动执行webbrowser control。与HttpWebRequest不同,这将与任何网站一起使用。
但如果你有更多的钱比时,可考虑使用商业component。
为什么这个标记的PHP? – 2010-12-17 21:38:19
发布到Apache PHP服务器??????? – 2010-12-17 21:44:05
是的,的确,这就是我创建页面的方式,表单将通过PHP – AdrenalineJunky 2010-12-18 14:56:18