大家好,小弟欲通过httpclient请求一个远程的dwr,我用工具fiddler(跟踪IE的工具)执行后,可以返回正常结果,请求如下(由于业务保密起见,url被我替换掉了,见谅):POST /test/dwr/exec/LoginService.getVaildCode.dwr HTTP/1.0
Accept: */*
Content-Type: text/plain
Referer: http://www.test.com/test/loginForm.jsp
Accept-Language: zh-cn
Connection: Keep-Alive
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; CIBA; .NET CLR 2.0.50727)
Host: www.test.com
Content-Length: 124
Pragma: no-cache
Cookie: __utma=43019386.104745520.1261384217.1261384217.1261385409.2; __utmb=43019386; __utmz=43019386.1261384217.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); __utmc=43019386; AMWEBJCT!%2Fportal!JSESSIONID=0000vp8J2WKmWHaiwYA6NzEIdg1:12i8cva0s; AMWEBJCT!%2Fportalframework!JSESSIONID=0000AnnN9kgzJPPSBfp_tgFs4Gk:12kqirar5
X-ProcessAndThread: iexplore.exe [3472; 1560]
X-Tickcount: 30238734callCount=1
c0-scriptName=LoginService
c0-methodName=getVaildCode
c0-id=5284_1261385571156
c0-param0=boolean:false
xml=true上面的信息是我从那个fiddler工具上复制过来的,它返回的结果是正常的,返回:var s0="5815"; 
然后我根据上面的信息,写了个请求,httpclient请求代码如下:    HttpClient httpclient = new HttpClient();
    PostMethod postMethod = new PostMethod("http://www.test.com//test/dwr/exec/LoginService.getVaildCode.dwr");
    NameValuePair[] postData = new NameValuePair[5];
    postData[0] = new NameValuePair("callCount", "1");
    postData[1] = new NameValuePair("c0-scriptName", "LoginService");
    postData[2] = new NameValuePair("c0-methodName", "getVaildCode"); 
    postData[3] = new NameValuePair("c0-param0", "boolean:false"); 
    postData[4] = new NameValuePair("xml", "true"); 
    postMethod.addParameters(postData);
    postMethod.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    postMethod.setRequestHeader("Cookie", "__utmc=43019386; __utma=43019386.104745520.1261384217.1261384217.1261384217.1; __utmb=43019386; __utmz=43019386.1261384217.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); AMWEBJCT!%2Fportal!JSESSIONID=0000J0BkeQlVdsPD9haPJs_n7Hq:12i8cva0s; AMWEBJCT!%2Fportalframework!JSESSIONID=0000OkuGG5FGCjUho0TKKS-lkPl:12kqirar5");    httpclient.executeMethod(postMethod);但是结果返回:var s0=null; 请问我的java代码有问题吗,是不是我少向服务器端发送了某些数据了呢?谢谢,谢谢~~
注:服务器端代码我看不到。

解决方案 »

  1.   

    不好意思,上面java代码第二行的url写错了,应该是一个斜杠,总之路径是没有问题的。谢谢。
      

  2.   

    强烈建议不要使用NameValuePair类了,网上很多用这个类的方法真的是非常笨,直接用参数实体提交的方式就OK了!下面这段参数写成一个字符串传到参数实体中,每行的末尾用“\n”连接起来。
    callCount=1
    c0-scriptName=LoginService
    c0-methodName=getVaildCode
    c0-id=5284_1261385571156
    c0-param0=boolean:false
    xml=true