using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; namespace ConsoleApplication1 { class Class1 { public string Post(string url, string postData) { string vystup = null; try { //Our postvars byte[] buffer = Encoding.ASCII.GetBytes(postData); //Initialisation, we use localhost, change if appliable HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url); //Our method is post, otherwise the buffer (postvars) would be useless WebReq.Method = "POST"; //We use form contentType, for the postvars. WebReq.ContentType = "application/x-www-form-urlencoded"; //The length of the buffer (postvars) is used as contentlength. WebReq.ContentLength = buffer.Length; //We open a stream for writing the postvars Stream PostData = WebReq.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! HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); //Let's show some information about the response Console.WriteLine(WebResp.StatusCode); Console.WriteLine(WebResp.Server); //Now, we read the response (the string), and output it. Stream Answer = WebResp.GetResponseStream(); StreamReader _Answer = new StreamReader(Answer); vystup = _Answer.ReadToEnd(); //Congratulations, you just requested your first POST page, you //can now start logging into most login forms, with your application //Or other examples. } catch (Exception ex) { Console.Write(ex.Message); } return vystup.Trim() + "\n"; } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Class1 cls1 = new Class1(); string sResponse = cls1.Post(@"http://test.webjet.com.au/webjettsatrial/unauth.aspx", @"PFPageID=FlightFinderTab&PFLayoutID=Layout&__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=%2FwEPDwUJNjEyNDMwODIwD2QWAgIBD2QWAgIJD2QWAmYPZBYCZg9kFgICBw9kFgJmD2QWEmYPDxYCHgdWaXNpYmxlaGQWAmYPZBYCZg9kFgICAw8PFgIeBFRleHQFAjMwZGQCAQ8PFgIfAGhkZAIEDxYCHwBoZAIGDw8WAh8AaGRkAgkPDxYCHwBoZGQCCg8PFgIfAGhkFgQCAQ8PFgIfAGhkZAIFDxYCHw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ZDb250ZW50VmlldyRQYWdlTGF5b3V0JEZsaWdodEZpbmRlclRhYiRIb21lcGFnZVRhYnMkQ2Fyc1RhYjEkYnRuU2VhcmNoBUhDb250ZW50VmlldyRQYWdlTGF5b3V0JEZsaWdodEZpbmRlclRhYiRIb21lcGFnZVRhYnMkSG90ZWxzVGFiMSRidG5TZWFyY2gFOkNvbnRlbnRWaWV3JFBhZ2VMYXlvdXQkRmxpZ2h0RmluZGVyVGFiJGJ0blNlYXJjaE1hdHJpeERvd24FMkNvbnRlbnRWaWV3JFBhZ2VMYXlvdXQkQm90dG9tTmF2JGJ0blByb2ZpbGVEZXRhaWxzBSlDb250ZW50VmlldyRQYWdlTGF5b3V0JEJvdHRvbU5hdiRidG5Mb2dvbgUpQ29udGVudFZpZXckUGFnZUxheW91dCRSaWdodE5hdiRidG5NeVRyaXAHB4SN%2BrUIjKsA31wzEsFFOrcfag%3D%3D&__PREVIOUSPAGE=oyjS_uJrr0APVQWbsJUmfEUfPFMytN5uT70VAkQkkzA1&TSAServerName=&TSALastClientIP=&mail=Enter+your+email+address&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24pnlTripType%24TripType=Return&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlDeparting=SYD&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlDestinationFinder_city=London+Area+Airports&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlDestinationFinder_cityCode=&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlTravelClass=ECONOMY&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlDepartureDate_day=10&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlDepartureDate_month=2010%2F09&ctlDepartureDate_selectedYear=2010&ctlDepartureDate_selectedMonth=9&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlReturnDate_day=20&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlReturnDate_month=2010%2F09&ctlReturnDate_selectedYear=2010&ctlReturnDate_selectedMonth=9&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlAirline1=0&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24ctlAirline2Hidden=0&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24uscNumberOfPassengers%24ctlAdults=1&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24uscNumberOfPassengers%24ctlChildren=0&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24uscNumberOfPassengers%24ctlInfants=0&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24SearchTodayTime=18&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlPickupCity_city=&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlPickupCity_cityCode=&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlPickupDate_day=3&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlPickupDate_month=2010%2F08&ctlPickupDate_selectedYear=2010&ctlPickupDate_selectedMonth=8&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlPickupTime=%3F&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlPickupLocation=NotSpecified&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlDropoffDate_day=4&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlDropoffDate_month=2010%2F08&ctlDropoffDate_selectedYear=2010&ctlDropoffDate_selectedMonth=8&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlDropoffTime=%3F&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlDropoffLocation=NotSpecified&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24ctlCarSize=Any&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24cblCarVendors%240=on&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24cblCarVendors%241=on&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24cblCarVendors%242=on&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24cblCarVendors%243=on&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24CarsTab1%24cblCarVendors%244=on&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24HotelDestination%24Countries=AU&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24HotelDestination%24Destinations=451&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24HotelDestination%24Locations=&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24HotelDestination%24CascadingDropDown1_ClientState=AU%3A%3A%3AAustralia&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24HotelDestination%24ccdCountryDestination_ClientState=451%3A%3A%3ASydney&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24HotelDestination%24ccdDestinationLocation_ClientState=%3A%3A%3A&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24ctlCheckInDate_day=2&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24ctlCheckInDate_month=2010%2F08&ctlCheckInDate_selectedYear=2010&ctlCheckInDate_selectedMonth=8&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24ctlCheckOutDate_day=3&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24ctlCheckOutDate_month=2010%2F08&ctlCheckOutDate_selectedYear=2010&ctlCheckOutDate_selectedMonth=8&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24ctlNumberOfAdults=1&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24ctlNumberOfChildren=0&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24HotelsTab1%24rblResultsViewMode=List&ContentView%24PageLayout%24FlightFinderTab%24currentView=SEARCH&Question=&QuestionLOH=&hiddenInputToUpdateATBuffer_CommonToolkitScripts=0&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24btnSearch.x=0&ContentView%24PageLayout%24FlightFinderTab%24HomepageTabs%24FlightsTab%24btnSearch.y=0"); Console.WriteLine(sResponse); Console.ReadLine(); } } }
Lưu ý để lấy dữ liệu từ Post dạng string dùng tool: Fiddler Web Debugger(v2.2.9.1) ,
sau đó truyền string đó vào phương thức Post trong Class1 -> public string Post(string url, string postData)
0 comments:
Post a Comment