求高手解决,关于web请求的一个问题

wangmohan44 2012-03-14 10:33:42
现在在ie上产生一个请求,通过struts2的action进入后台代码,执行一半后,我现在想在action中通过httpclient又去请求另一个地址(只要是一个http请求都可以),但是 这个httpclient的请求确发送失败,求解决。怎样在一个用struts2的框架中再去发生一个http请求,去请求别的地址。
...全文
126 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
这个changeDocumentBookState是封装成一个service了
@Service
public class TaskServiceImpl implements TaskService
{
private Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);
private HttpClient httpClient = null;

@Autowired
public TaskServiceImpl(TaskMappper taskMapper, Configure configure)
{
HttpParams params = new BasicHttpParams();
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
cm.setDefaultMaxPerRoute(50);
cm.setMaxTotal(50);
httpClient = new DefaultHttpClient(cm, params);

}
[Quote=引用 10 楼 wangmohan44 的回复:]

楼上最重要的HttpResponse remoteResponse = this.httpClient.execute(httpUriRequest)这句话 httpClient 对象是哪里来的 是那个对象
[/Quote]httpClient4.1.2
wangmohan44 2012-03-15
  • 打赏
  • 举报
回复
可能之前我没说清楚,我要得是在一个请求中,在进行第二次请求,拿到第二次请求的结果后返回给第一次请求,第一次请求在响应给页面。 这个还有个前提,第一次请求是用struts2 第二次用得httpclient ,但是我搞不出来,所以求助大家
wangmohan44 2012-03-15
  • 打赏
  • 举报
回复
楼上最重要的HttpResponse remoteResponse = this.httpClient.execute(httpUriRequest)这句话 httpClient 对象是哪里来的 是那个对象
liuc0317 2012-03-15
  • 打赏
  • 举报
回复
发送失败后,就会有数据返回啊。那就是失败的代码,你需要捕获这个异常。然后处理
  • 打赏
  • 举报
回复
@RequestMapping(value = "changeDocumentBookState", method = RequestMethod.POST)
public String changeDocumentBookState(@RequestParam("operator") int operator, @RequestParam("documentBookId") long documentBookId, HttpServletRequest request)
{
HttpSession session = request.getSession();

CPUser user = (CPUser)session.getAttribute(Constants.LOGIN_USER);

long cpId = user.getEntityID();

String usercodes =request.getParameter("usercodes");

MallInterfaceJson json =new MallInterfaceJson();

json.setCpid(cpId+"");

json.setMagid(documentBookId+"");

json.setItname(OperatorState.operatorStatus(operator).getName());

taskService.changeDocumentBookState(json);

}

changeDocumentBookState是一个httpclient调接口的方法
public boolean changeDocumentBookState(MallInterfaceJson json)
{
String targetUrl = configure.getMallInterfaceAddress()+configure.getMallIfGoods();// 接口地址

HttpUriRequest httpUriRequest = null;
try
{
httpUriRequest = new HttpPost(targetUrl);

String abc = mapper.writeValueAsString(json);

((HttpPost) httpUriRequest).setEntity(new StringEntity(abc, HTTP.UTF_8));

HttpResponse remoteResponse = this.httpClient.execute(httpUriRequest);

int responseState = remoteResponse.getStatusLine().getStatusCode();

if (HttpStatus.SC_OK == responseState)
{
// 请求成功
HttpEntity responseEntity = remoteResponse.getEntity();

if (responseEntity != null)
{// 返回信息

String strEntity = EntityUtils.toString(responseEntity, HTTP.UTF_8);

JsonNode node = mapper.readTree(strEntity);

String result =node.get("backstatus").toString();

result =result.substring(1,result.length()-1);

if (result.equalsIgnoreCase("1"))
{
return true;
}
}
}
} catch (Exception e)
{
e.printStackTrace();
logger.warn("调用接口失败{}", e.toString());
return false;
}
return false;
}
你参考参考可以试试
dryZeng 2012-03-15
  • 打赏
  • 举报
回复
你这需求有问题。

为什么要在java程序逻辑中请求其它web地址呢?为什么你不直接调用那一请求的action方法呢?
TommyWu01 2012-03-15
  • 打赏
  • 举报
回复
用session
Acesidonu 2012-03-14
  • 打赏
  • 举报
回复
请求转发
zuxianghuang 2012-03-14
  • 打赏
  • 举报
回复
你最终是要在哪里返回到前台呢?
重定向,是个解决方法
Jan丶X 2012-03-14
  • 打赏
  • 举报
回复
可以再你的action执行完之后的result重定义一个页面
结贴是美德 2012-03-14
  • 打赏
  • 举报
回复
重定向吧。。。

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧