http - c# Downloading PNG file from website and sending cookies in Request? -
hello trying download .png image website, need headers sent in request download image. end goal image loaded directly stream, rather saving file.
this code image's html with:
public string request(string type, string page, string data = "", string referer = "") { using(tcpclient tcp = new tcpclient(this.sitename, this.port)) { byte[] headers = encoding.default.getbytes(this.makeheaders(type, page, referer, data)); using (networkstream ns = tcp.getstream()) { ns.write(headers, 0, headers.length); using (streamreader sr = new streamreader(ns, encoding.default)) { string html = sr.readtoend(); return html; } } } }
the headers send this:
get /image.php http/1.1 host: greymatter.com user-agent: mozilla/5.0 (windows nt 6.1; wow64; rv:22.0) gecko/20100101 firefox/22.0 accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-language: en-us,en;q=0.5 connection: close cache-control: max-age=0 cookie: phpsessid=s0v17eafosj5ctb5v6keu3lh57; __cfduid=d10aef4fcfd052e1f59fba9832cedf4491374611636
the png file save locally starts with:
67f รข€°png
the saved png file corrupt - can't viewed! don't understand why happening?
the request of php document. individual assets of web site downloaded separately. single tcp request may not cover intending do. i'd recommend fiddler looking @ calls made requesting web page.
also i'd @ using image.fromstream() convert stream png. (just pref. working byte arrays , streams data has been particularly error prone me.)
its used this.
byte[] imagedata = myrequestfunction(url); //myrequestfunction function here memorystream stream = new memorystream(imagedata); image img = image.fromstream(stream); stream.close();
additionally web requests made doing. example might help.
how read picture url , show on page
Comments
Post a Comment