我有一段python的代码,想用JAVA来实现,不知道该如何搞,希望哪位不吝赐教,谢谢了。
python代码如下:import httplib, urllibparams = urllib.urlencode( {
    'user': xxxx,
    'password': xxxx,
    } )headers = { "Content-type": "application/x-www-form-urlencoded" }
conn = httplib.HTTPConnection("xxxx.xxxx.com")
conn.request("POST", "/char/xxxx.xml.aspx", params, headers)response = conn.getresponse()
print response.status, response.reasondata = response.read()
print dataconn.close
其中的“xxxx”代表某网站的地址。

解决方案 »

  1.   

    用J2EE里Servlet来实现
    public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String user = request.getParameter("user");
    String password = request.getParameter("password");
    System.out.println("USER: " + user + " PASSWORD:" + password);
    }
    }
      

  2.   

    用apache的commons里面的httpclient
      

  3.   

    http://qnr.cn/pc/java/fudao/zhuanye/200911/246160.html
      

  4.   

    主要思想明白了,不过还有一点问题,我先贴出我的代码:
    import java.io.IOException;import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;/** 
     * @author  
     * @version 创建时间:2010-4-10 下午07:33:17 
     */public class MYAPI {
    private static String HOME_URL = "http://api.xxxx.com";
            private static String DET_URL = "http://api.xxx.com/char/xxx.xml.aspx";

    public static void main(String[] args) { HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(URLSTR);

    Header header = new Header("Content-type","application/x-www-form-urlencoded");
    // 填入各个表单域的值
    NameValuePair[] data = { new NameValuePair("userid", "xxxx"),
    new NameValuePair("password", "xxxx") };
    // 将表单的值放入postMethod中
    postMethod.setRequestBody(data);
    // 执行postMethod
    int statusCode;
    try {
    statusCode = httpClient.executeMethod(postMethod);
    // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
    // 301或者302
    if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
        // 从头中取出转向的地址
        Header locationHeader = postMethod.getResponseHeader("location");
        String location = null;
        if (locationHeader != null) {
         location = locationHeader.getValue();
         System.out.println("The page was redirected to:" + location);
        } else {
         System.err.println("Location field value is null.");
        }
    System.out.println(postMethod.getResponseBodyAsString());
        return;
    }
    } catch (HttpException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }
    }问题如下:
    我应该已经正常登入主网页了,经过分析得知主网页转向到了另一个网页,而且我也拿到那个网页的地址。我通过登录后主要是想获取另一个网页的数据,就是前定义的DET_RUL,该数据文件在主网所在的主机里,是个asp的网页,内容是xml的数据。不知道如何获取。
      

  5.   

    那个DET_URL = "/char/xxx.xml.aspx";
    应该是这样的定义,应该是个URI,上面的代码里,不知道怎么去获取这个aspx的文件内容。
      

  6.   

    我试着去用FileRequestEntity 去获取,可是提示说:
    2010-4-20 14:19:10 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
    信息: I/O exception (java.io.FileNotFoundException) caught when processing request: \char\xxx.xml.aspx (系统找不到指定的路径。)