c# - Cannot send a content-body with this verb-type with GET request -
i'm receiving request through webapi, , trying resend site.
the goal receive request, example: http://localhost:9999/#q=test. , forward real site:(for test set google.com) http://google.com/#q=test
i've following code:
protected override async task<httpresponsemessage> sendasync(httprequestmessage request, system.threading.cancellationtoken cancellationtoken) { string url = request.requesturi.pathandquery; uribuilder forwarduri = new uribuilder(_otherwebsitebase); forwarduri.path = url; if (request.method == httpmethod.get) { //request.method = httpmethod.post; } request.requesturi = forwarduri.uri; request.headers.host = forwarduri.host; return await _client.sendasync(request, httpcompletionoption.responseheadersread);//_client httpclient }
currently got system.net.protocolviolationexception
states: cannot send content-body verb-type.
but input request request(and should request). if put post request, don't have exception anymore, google says don't expect post request.
so why exception coming? idea on how fix it?
i ended creating copy of initial request, , sending again:
protected override async task<httpresponsemessage> sendasync(httprequestmessage request, system.threading.cancellationtoken cancellationtoken) { string url = request.requesturi.pathandquery; uribuilder forwarduri = new uribuilder(_otherwebsitebase); forwarduri.path = url; httprequestmessage newrequest = request.clone(forwarduri.uri.tostring()); httpresponsemessage responsemessage = await _client.sendasync(newrequest); return responsemessage; }
the clone method following, inspired question: how forward httprequestmessage server
public static httprequestmessage clone(this httprequestmessage req, string newuri) { httprequestmessage clone = new httprequestmessage(req.method, newuri); if (req.method != httpmethod.get) { clone.content = req.content; } clone.version = req.version; foreach (keyvaluepair<string, object> prop in req.properties) { clone.properties.add(prop); } foreach (keyvaluepair<string, ienumerable<string>> header in req.headers) { clone.headers.tryaddwithoutvalidation(header.key, header.value); } clone.headers.host = new uri(newuri).authority; return clone; }
Comments
Post a Comment