我希望有人能幫助我一段令我困惑的問題。是否可以將數據從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