c# - Why i'm getting exception No connection could be made when trying to send data to a server? -
this method:
private void senddatatoserver() { webrequest request = webrequest.create("http://10.0.0.2:8080 "); request.method = "post"; string postdata = "this test posts string web server."; byte[] bytearray = encoding.utf8.getbytes(postdata); request.contenttype = "application/x-www-form-urlencoded"; request.contentlength = bytearray.length; stream datastream = request.getrequeststream(); datastream.write(bytearray, 0, bytearray.length); datastream.close(); webresponse response = request.getresponse(); console.writeline(((httpwebresponse)response).statusdescription); datastream = response.getresponsestream(); streamreader reader = new streamreader(datastream); string responsefromserver = reader.readtoend(); console.writeline(responsefromserver); reader.close(); datastream.close(); response.close(); }
maybe port 8080 didn't use right ?
the string should send server should in format:
"http://10.0.0.2:8080/?say=hello world"
the same when type in chrome browser when type: http://10.0.0.2:8080/?say=hello world
the part http://10.0.0.2:8080/?say= should never change. text after hello world if change hello or else. left part http://10.0.0.2:8080/?say= should never change.
the exception on line:
stream datastream = request.getrequeststream();
no connection made because target machine actively refused it
i checked in chrome can send hello world: http://10.0.0.2:8080/?say=hello world
http post messages can include body, block of data, in example postdata
variable sent body of message.
besides block of data, can send query string, number of key/value pairs included in request uri, in form ?key1=value1&key2=value2
.
the server can choose process either query string, body, or both, , server you're communicating chooses process query string.
however error message "no connection made because target machine actively refused it" more caused space character added request uri: "http://10.0.0.2:8080 "
.
Comments
Post a Comment