c# - The same code works using HttpWebRequest but not using HttpRequestMessage -
i've created httpclient
i'm using sending requests:
public static void initialize() { handler = new httpclienthandler() { usecookies = false, allowautoredirect = true }; http = new httpclient(handler) { baseaddress = new uri("http://csgolounge.com/mytrades") }; http.defaultrequestheaders.add("user-agent", "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/42.0.2311.135 safari/537.36"); }
after i'm creating instance of custom class stores cookies string account (something id=xxxxxxxx; tkz=xxxxxxxxxx; token=xxxxxxxxxxx
.
that's how i'm sending post request:
public async task bump() { //if (bumpable) //{ var req = new httprequestmessage(httpmethod.post, "http://www.csgolounge.com/ajax/bumptrade.php"); req.headers.add("cookie", cookiestring); req.headers.add("x-requested-with", "xmlhttprequest"); req.headers.add("referer", "http://csgolounge.com/mytrades"); //not sure if i've run out of smart ideas long time ago /*dictionary<string, string> postdata = new dictionary<string, string>() { {"trade", offer_id} }; var encoded = new formurlencodedcontent(postdata); */ req.content = new stringcontent("&trade="+offer_id, encoding.utf8, "application/x-www-form-urlencoded"); //desperation.. decided change encoded dictionary stringcontent var res = await steamaccount.http.sendasync(req); var html = await res.content.readasstringasync(); //} }
i don't what's wrong code. seems correct me.
also, when set allowautoredirect = false
returns 301: moved permanently error, while returns 200 no html no matter pass content.
what doing wrong?
edit: here's javascript
function i'm basing request on:
function bumptrade(trade) { $.ajax({ type: "post", url: "ajax/bumptrade.php", data: "trade=" + trade }); }
i've worked more complex ajax before, doesn't seem work no matter do.
edit: i've lost patience , switched httpwebrequest
instead.
now method looks this:
public async task bumplegacy() { while (true) { try { httpwebrequest httpwebrequest = (httpwebrequest)webrequest.create("http://csgolounge.com/ajax/bumptrade.php"); var cc = new cookiecontainer(); matchcollection mc = regex.matches(account.cookiestring, @"\s?([^=]+)=([^;]+);"); foreach (match m in mc) cc.add(new cookie(m.groups[1].value, m.groups[2].value, "/", "csgolounge.com")); httpwebrequest.cookiecontainer = cc; byte[] bytes = encoding.ascii.getbytes("trade=" + offer_id); httpwebrequest.referer = "http://csgolounge.com/mytrades"; httpwebrequest.headers.add("x-requested-with", "xmlhttprequest"); httpwebrequest.method = "post"; httpwebrequest.contenttype = "application/x-www-form-urlencoded; charset=utf-8"; httpwebrequest.useragent = "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/42.0.2311.135 safari/537.36"; httpwebrequest.contentlength = (long)bytes.length; var g = await httpwebrequest.getrequeststreamasync(); await g.writeasync(bytes, 0, bytes.count()); g.close(); var res = await httpwebrequest.getresponseasync(); res.close(); break; } catch { } } }
maybe i'm dumb me doesn't seem different. there key differences can cause?
here code 1 of working systems submits post request through httpclient
.
[route("resource")] public async task<dynamic> createresource([frombody]resource resource) { if (resource == null) return badrequest(); dynamic response = null; resource.topic = getdatafromsomewhereelse(); var message = new postmessage(resource).bodycontent; dynamic postrequest = new { message = message }; var post = jsonconvert.serializeobject(postrequest); httpcontent content = new stringcontent(post, encoding.utf8, "application/json"); using (var client = new httpclient()) { client.timeout = timespan.fromminutes(1); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new system.net.http.headers.mediatypewithqualityheadervalue("application/json")); try { client.baseaddress = @"http://localhost:51145/"; httpresponsemessage postresponse = await client.postasync("resource", content); //"resource" route exposed on remote host string json = await postresponse.content.readasstringasync(); if (postresponse.statuscode == httpstatuscode.badrequest) return badrequest(); if (postresponse.statuscode == httpstatuscode.internalservererror) return internalservererror(); if (postresponse.statuscode == httpstatuscode.notfound) return notfound(); return json; } catch(exception ex) { return internalservererror(ex); } } }
[edit] "postmessage" modified remove domain-specific details. here how bodycontent
defined inside real "postmessage" solution, provide enough context understand "message" , how works sample.
public string bodycontent { { string content = ""; type type = this.gettype(); assembly assembly = assembly.getexecutingassembly(); string resource = string.format("{0}.{1}", type.namespace, this.embeddedresourcename); stream stream = assembly.getmanifestresourcestream(resource); streamreader reader = new streamreader(stream); content = reader.readtoend(); return content; } }
...and here postrequest
(again, domain-specific details trimmed)
public class postrequest { public string message { get;set; } }
Comments
Post a Comment