a8892903a 2011-04-05 00:03
浏览 1144
已采纳

socket访问页面时返回了301重定向的代码,我该怎么解决?

我在网上查了查,好像应该在次请求一下返回的location,但我不知道该怎么改代码.麻烦高手帮我看看.没多少分了

下面是我的代码,我请求的是一个测姻缘的网站..

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URLEncoder;

public class GetYinYuan {

public static String getResult(){
    StringBuffer sb=new StringBuffer();
    String boy="张三";
    String girl="李四";
    try {
        String data="boy="+URLEncoder.encode(boy,"UTF-8")+"&girl="+URLEncoder.encode(girl, "UTF-8");
        String hostname="www.guabu.com";
        int port=80;
        InetAddress iaddr=InetAddress.getByName(hostname);
        Socket socket=new Socket(iaddr,port);
        String path="/yinyuan/yuanfen.asp?"+data;
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GBK"));
        bw.write("GET "+path+" HTTP/1.0\r\n");
        bw.write("\r\n");
        bw.write(data);
        bw.flush();
        BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
        String line;
        boolean flag=false;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }
        bw.close();
        br.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}
public static void main(String[] args) {
    getResult();
}

}

下面是返回的代码
HTTP/1.1 301 Moved Permanently
Content-Length: 148
Content-Type: text/html
Location: http://www.huochepiao.com
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Mon, 04 Apr 2011 15:50:52 GMT
Connection: close

Document Moved

Object Moved

This document may be found here
  • 写回答

7条回答 默认 最新

  • shadabing 2011-04-06 22:10
    关注

    好吧: 66626830

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了