c# - How to do a HTTP GET request using async and await -


i'm trying create function takes url parameter , returns response it, give me error in webresponse responseobject intialization line

the exception an exception of type 'system.net.protocolviolationexception' occurred in mscorlib.ni.dll not handled in user code

public static async task<string> get(string url) {     var request = webrequest.create(new uri(url)) httpwebrequest;     request.method = "get";     request.contenttype = "application/json";     webresponse responseobject = await task<webresponse>.factory.fromasync(request.begingetresponse, request.endgetresponse, request);     var responsestream = responseobject.getresponsestream();     var sr = new streamreader(responsestream);     string received = await sr.readtoendasync();      return received; } 

you can't set contenttype request aren't sending data server.

content-type = mime type of body of request (used post , put requests).

this source of protocolviolationexception.

it looks want set accept header instead.

accept = content-types acceptable response

(as per wikipedia)

try changing code to:

request.accept = "application/json"; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -