Post Data to site

Topics: Developer Forum
Aug 23, 2007 at 11:04 PM
Hi there

I can see a method to GET data from website but how would I POST data to the website and get the response back? I've posted data before using my own code which looks like:

protected string PostGetHtmlSite(string url, string postData, out bool responseOK)
{
string htmlResponse = string.Empty;
responseOK = false;
try
{
HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(url);
HttpWebResponse webResponse;
//Our postvars
byte[] buffer = Encoding.ASCII.GetBytes(postData);
// need a cookie container to store cookies
webRequest.CookieContainer = cookieContainer;
//Our method is post, otherwise the buffer (postvars) would be useless
webRequest.Method = POST;
webRequest.AllowWriteStreamBuffering = false;
webRequest.UserAgent = USER_AGENT;
//We use form contentType, for the postvars.
webRequest.ContentType = CONTENT_TYPE;
//The length of the buffer (postvars) is used as contentlength.
webRequest.ContentLength = 0;
webRequest.ContentLength = buffer.Length;
//We open a stream for writing the postvars
webRequest.ReadWriteTimeout = 5000;
Stream PostData = webRequest.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
webResponse = (HttpWebResponse) webRequest.GetResponse();
webResponse.Cookies = webRequest.CookieContainer.GetCookies(webRequest.RequestUri);
//Let's show some information about the response
if (webResponse.StatusCode.ToString() == OK)
{
Stream stream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
htmlResponse = streamReader.ReadToEnd();
if (htmlResponse != string.Empty && htmlResponse.Contains("</html>"))
{
responseOK = true;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return htmlResponse;
}

This code posts data to a specified url and then reads the response coming back. But I can't see any way of doing this using the agility pack.

Regards
Aug 31, 2007 at 1:30 AM
Edited Aug 31, 2007 at 1:30 AM
Take a look at the following link :

http://www.dotnetjunkies.com/WebLog/joshuagough/archive/category/2693.aspx

Joshua has a nice way of handling Post in the SubmitFormValues method :
-- Begin copy --
//Add this to the header :
using System.Collections.Specialized;
using System.Text;

//Class methods :
public HtmlDocument SubmitFormValues(NameValueCollection fv, string url)
{
// Attach a temporary delegate to handle attaching
// the post back data
PreRequestHandler handler = delegate(HttpWebRequest request)
{
string payload = AssemblePostPayload(fv);
byte[] buff = Encoding.ASCII.GetBytes(payload.ToCharArray());
request.ContentLength = buff.Length;
request.ContentType = "application/x-www-form-urlencoded";
System.IO.Stream reqStream = request.GetRequestStream();
reqStream.Write(buff, 0, buff.Length);
return true;
};
this.PreRequest += handler;
HtmlDocument doc = this.Load(url, "POST");
this.PreRequest -= handler;
return doc;
}

private string AssemblePostPayload(NameValueCollection fv)
{
StringBuilder sb = new StringBuilder();
foreach (String key in fv.AllKeys)
{
sb.Append("&" + key + "=" + fv.Get(key));
}
return sb.ToString().Substring(1);
}

-- End copy --

This can go into the HTMLWeb class to help post Data -

HTH
Jul 20, 2011 at 10:18 AM

hi avadul

I have post following my code:

HtmlWeb htmlweb = new HtmlWeb();
            NameValueCollection postData = new NameValueCollection();
            postData.Add("__aa","14");
            postData.Add("__pIndex","1");
            postData.Add("__pSize","20");
            postData.Add("__date","");
            postData.Add("__catID", "21");

Now i want get data that response when debug by firebug 

{ "TotalItem":168, "TotalPage":9, "d": [{"ArticleId": 6729,"Title": "205: Cấp Giấy chứng nhận Th&#224;nh vi&#234;n lưu k&#253;","Href": "/6729p4c22/205-cap-giay-chung-nhan-thanh-vien-luu-ky.htm","Date": "15/07/2011"},{"ArticleId": 6724,"Title": "015: Đăng k&#253; bổ sung Danh s&#225;ch ban l&#227;nh đạo phụ tr&#225;ch hoạt động lưu k&#253;","Href": "/6724p4c22/015-dang-ky-bo-sung-danh-sach-ban-lanh-dao-phu-trach-hoat-dong-luu-ky.htm","Date": "15/07/2011"},{"ArticleId": 6716,"Title": "006: Cấp Giấy chứng nhận chi nh&#225;nh hoạt động lưu k&#253; sửa đổi","Href": "/6716p4c22/006-cap-giay-chung-nhan-chi-nhanh-hoat-dong-luu-ky-sua-doi.htm","Date": "14/07/2011"},{"ArticleId": 6715,"Title": "070: Thay đổi Th&#224;nh vi&#234;n Hội đồng quản trị","Href": "/6715p4c22/070-thay-doi-thanh-vien-hoi-dong-quan-tri.htm","Date": "14/07/2011"},{"ArticleId": 6714,"Title": "032: Bổ sung nghiệp vụ kinh doanh chứng kho&#225;n","Href": "/6714p4c22/032-bo-sung-nghiep-vu-kinh-doanh-chung-khoan.htm","Date": "14/07/2011"},{"ArticleId": 6713,"Title": "032: Tăng vốn điều lệ","Href": "/6713p4c22/032-tang-von-dieu-le.htm","Date": "14/07/2011"},{"ArticleId": 6709,"Title": "057: Cấp Giấy chứng nhận Th&#224;nh vi&#234;n lưu k&#253; sửa đổi","Href": "/6709p4c22/057-cap-giay-chung-nhan-thanh-vien-luu-ky-sua-doi.htm","Date": "13/07/2011"},{"ArticleId": 6708,"Title": "006: Uỷ quyền k&#253; chứng từ lưu k&#253;","Href": "/6708p4c22/006-uy-quyen-ky-chung-tu-luu-ky.htm","Date": "13/07/2011"},{"ArticleId": 6707,"Title": "069: Uỷ quyền k&#253; chứng từ lưu k&#253;","Href": "/6707p4c22/069-uy-quyen-ky-chung-tu-luu-ky.htm","Date": "13/07/2011"},{"ArticleId": 6682,"Title": "085: Uỷ quyền k&#253; chứng từ lưu k&#253;","Href": "/6682p4c22/085-uy-quyen-ky-chung-tu-luu-ky.htm","Date": "12/07/2011"},{"ArticleId": 6667,"Title": "072: Cấp Giấy chứng nhận Th&#224;nh vi&#234;n lưu k&#253; sửa đổi","Href": "/6667p4c22/072-cap-giay-chung-nhan-thanh-vien-luu-ky-sua-doi.htm","Date": "11/07/2011"},{"ArticleId": 6666,"Title": "065: Ủy quyền k&#253; chứng từ lưu k&#253;","Href": "/6666p4c22/065-uy-quyen-ky-chung-tu-luu-ky.htm","Date": "11/07/2011"},{"ArticleId": 6594,"Title": "004: Uỷ quyền k&#253; chứng từ lưu k&#253;","Href": "/6594p4c22/004-uy-quyen-ky-chung-tu-luu-ky.htm","Date": "08/07/2011"},{"ArticleId": 6590,"Title": "048: Đăng k&#253; bổ sung Danh s&#225;ch Ban l&#227;nh đạo phụ tr&#225;ch hoạt động lưu k&#253;","Href": "/6590p4c22/048-dang-ky-bo-sung-danh-sach-ban-lanh-dao-phu-trach-hoat-dong-luu-ky.htm","Date": "07/07/2011"},{"ArticleId": 6567,"Title": "083: Đăng k&#253; bổ sung Danh s&#225;ch Th&#224;nh vi&#234;n Ban gi&#225;m đốc phụ tr&#225;ch hoạt động lưu k&#253;","Href": "/6567p4c22/083-dang-ky-bo-sung-danh-sach-thanh-vien-ban-giam-doc-phu-trach-hoat-dong-luu-ky.htm","Date": "05/07/2011"},{"ArticleId": 6566,"Title": "083: Thay đổi cơ cấu nh&#226;n sự","Href": "/6566p4c22/083-thay-doi-co-cau-nhan-su.htm","Date": "05/07/2011"},{"ArticleId": 6565,"Title": "019: Thay đổi cơ cấu nhận sự","Href": "/6565p4c22/019-thay-doi-co-cau-nhan-su.htm","Date": "05/07/2011"},{"ArticleId": 6564,"Title": "019: Đăng k&#253; bổ sung Danh s&#225;ch Th&#224;nh vi&#234;n Ban gi&#225;m đốc phụ tr&#225;ch hoạt động lưu k&#253;","Href": "/6564p4c22/019-dang-ky-bo-sung-danh-sach-thanh-vien-ban-giam-doc-phu-trach-hoat-dong-luu-ky.htm","Date": "05/07/2011"},{"ArticleId": 6534,"Title": "086: Uỷ quyền k&#253; chứng từ lưu k&#253;","Href": "/6534p4c22/086-uy-quyen-ky-chung-tu-luu-ky.htm","Date": "02/07/2011"},{"ArticleId": 6504,"Title": "008: Đăng k&#253; bổ sung Danh s&#225;ch Th&#224;nh vi&#234;n Ban gi&#225;m đốc chi nh&#225;nh phụ tr&#225;ch hoạt động lưu k&#253;","Href": "/6504p4c22/008-dang-ky-bo-sung-danh-sach-thanh-vien-ban-giam-doc-chi-nhanh-phu-trach-hoat-dong-luu-ky.htm","Date": "29/06/2011"}]}
can i do ?
 
HtmlAgilityPack.HtmlDocument document = htmlweb.SubmitFormValues(postData, "http://vsd.vn/Ajax/action.ashx");