我要做一个从天气预报的网站取得数据的小程序,要求是用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 xmlns="http://www.webserviceX.NET">string </string> 我做了这个小程序,但无法得到正确结果, 我是按照它提供的格式写道out.write()中的啊,问题在哪?
import java.io.*;
import java.net.*;public class TCPClient {
public static void main(String argv[]) throws Exception {
final int HTTP_PORT = 80;
Socket socket = new Socket("www.webservicex.net", HTTP_PORT);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in
= new BufferedReader(new InputStreamReader(socket.
getInputStream()));
out.write("POST /globalweather.asmx/GetCitiesByCountry HTTP/1.1 Host: www.webservicex.net Content-Type: application/x-www-form-urlencoded Content-Length: 1024 CountryName=china");
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());
}
}
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 xmlns="http://www.webserviceX.NET">string </string> 我做了这个小程序,但无法得到正确结果, 我是按照它提供的格式写道out.write()中的啊,问题在哪?
import java.io.*;
import java.net.*;public class TCPClient {
public static void main(String argv[]) throws Exception {
final int HTTP_PORT = 80;
Socket socket = new Socket("www.webservicex.net", HTTP_PORT);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in
= new BufferedReader(new InputStreamReader(socket.
getInputStream()));
out.write("POST /globalweather.asmx/GetCitiesByCountry HTTP/1.1 Host: www.webservicex.net Content-Type: application/x-www-form-urlencoded Content-Length: 1024 CountryName=china");
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());
}
}
应该 out.write("POST /globalweather.asmx/GetCitiesByCountry HTTP/1.1\r\n");
out.write("Host: www.webservicex.net Content-Type: application/x-www-form-urlencoded\r\n")
以此类推记住严格遵守协议规范,要换行
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.1\r\n");
out.write("Host: www.webservicex.net\r\n");
out.write("Content-Type: application/x-www-form-urlencoded\r\n");
out.write("Content-Length: 1024\r\n");
out.write("\r\n");
out.write("CountryName=china\r\n");
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());
}
}
换种 方式 ,这种 一般就是 java 使用 webService 方法 很多,直接使用HTTP 协议的 还有 使用 axis 的 ,方法 很多,
然后用pw.println("你要写到服务器的内容"); 试试看。
建议楼主用个线程不停的去读服务器的返回,并打印在控制台,便于查找问题