我要做一个从天气预报的网站取得数据的小程序,要求是用socket来做,那个服务器要求的格式是这样的:
POST /globalweather.asmx/GetCitiesByCountry HTTP/1.1
Host: www.webservicex.net
Content-Type: application/x-www-form-urlencoded
Content-Length: length
CountryName=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
string
我做了这个小程序,返回了部分的头信息,但最主要的string返回内容没有给我, 我是按照它提供的格式的啊,问题在哪?
还有就是我把请求的格式变成HTTP/1.0 才能运行,如果按格式要求的写成HTTP/1.1 ,根本就运行不了,这是什么原因?
import java.io.*;
import java.net.*;
public class TCPClient {
public static void main(String argv[])throws Exception {
final int HTTP_PORT=80;
Socket socket1 = new Socket("www.webservicex.net", HTTP_PORT);
BufferedWriter out= new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream()));
BufferedReader in= new BufferedReader(new InputStreamReader(socket1.getInputStream()));
out.write("POST /globalweather.asmx/GetCitiesByCountry HTTP/1.0\r\n");
out.write("Host: www.webservicex.net\r\n");
out.write("Content-Type: application/x-www-form-urlencoded\r\n");
String body = "CountryName=china\r\n";
out.write("Content-Length: " + body.length() + "\r\n");
out.write("\r\n");
out.write(body);
out.flush();
String line;
StringBuffer sb=new StringBuffer();
while((line=in.readLine())!= null) {
sb.append(line+"\r\n");
}
out.close();
in.close();
System.out.println(sb.toString());
}
}