PDA

View Full Version : نحوه ارسال درخواست به سرور از طریق xml



amirjigili
دوشنبه 03 آذر 1393, 11:02 صبح
سلام دوستان من میخوام ی درخواست به سرور زیر بفرستم ولی فقط سمپل جاوا داره کسی میتونه راهنماییم کنه معادل بخش برنامه نویسی سوکت تحت سی شارپش چی میشه فقط معادل بخش سوکنش رو بگید تو سی شارپ چیه کافیه و این که دقیقا روند اتصال چه مراحلی رو داره.


public static String sendXml(String requestString) throws Exception { String theHost = "api.travelfusion.com";
byte[] requestBytes = requestString.getBytes("UTF-8");
Socket httpSocket = new Socket(theHost, 80);
try {
BufferedOutputStream httpOutput = new BufferedOutputStream(httpSocket.getOutputStream()) ;
httpOutput.write("POST /Xml HTTP/1.0\r\n".getBytes());
httpOutput.write("Content-Type: text/xml\r\n".getBytes());
httpOutput.write(("Host: " + theHost + "\r\n").getBytes());
httpOutput.write("Accept-Encoding: gzip" + "\r\n");
httpOutput.write(("Content-Length: " + requestBytes.length + "\r\n").getBytes());
httpOutput.write("\r\n".getBytes());
httpOutput.write(requestBytes);
httpOutput.flush();

BufferedInputStream httpInput = new BufferedInputStream(httpSocket.getInputStream());
ByteArrayOutputStream output = new ByteArrayOutputStream();
int count = 0;
while (true) {
int b = httpInput.read();
if (b == -1) {
break;
}
output.write(b);
}
byte[] response = output.toByteArray();
return new String(response, "UTF-8");
}
finally {
httpSocket.close();
} }