PEN3GU 2021-09-23 08:44 采纳率: 57.1%
浏览 551
已结题

java http接口在本地能跑通,在服务器上跑就不通

httpExchange写的http post接口,通过get一个url,返回json,再通过接口返回。在本地都能用postman通过,但是放到服务器上就不通。

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import java.io.OutputStream;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * 处理/myserver路径请求的处理器类
 */
public class jbxx implements HttpHandler {
    @Override
    public void handle(HttpExchange httpExchange) {
        try {
            StringBuilder responseText = new StringBuilder();
            responseText.append(getRequestParam(httpExchange));
            handleResponse(httpExchange, responseText.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 获取请求参数
     * @param httpExchange
     * @throws Exception
     */
    private JSONObject getRequestParam(HttpExchange httpExchange) throws Exception {
        String paramStr = "";
        paramStr = httpExchange.getRequestURI().getQuery();
        String[] s1=paramStr.split("=");
        JSONObject obj4=null;
        if(s1[0].equals("sfz")){
            tool tool = new tool();
        String url ="";
        String timeStamp = Long.toString(System.currentTimeMillis() / 100000);
        String param ="appId=8&appName=DExchange&timeStamp="+timeStamp;
        String a = tool.sendGet(url,param);
        JSONObject obj= JSON.parseObject(a);
        String appcode =obj.getString("data");
        String url2=url;
        String bm = tool.bm(s1[1]);
        String param2 ="zjhm="+bm+"&appCode="+appcode+"" +
                "&apiToken=88";
        String b = tool.sendGet(url2,param2);
        JSONObject obj2= JSON.parseObject(b);
        JSONArray obj3= JSON.parseArray(obj2.getString("data"));
        if (obj3.size() == 0) {
            String resultMsg ="{\"resultMsg\":\"没有查询到对应的信息\"}";
            obj4= JSON.parseObject(resultMsg);
        }else {
            obj4 = JSON.parseObject(obj3.getString(0));
        }
    }
        return obj4;
    }
    /**
     * 处理响应
     * @param httpExchange
     * @param responsetext
     * @throws Exception
     */
    private void handleResponse(HttpExchange httpExchange, String responsetext) throws Exception {
        //生成html
        StringBuilder responseContent = new StringBuilder();
        responseContent.append(responsetext);
        String responseContentStr = responseContent.toString();
        byte[] responseContentByte = responseContentStr.getBytes("utf-8");
        //设置响应头,必须在sendResponseHeaders方法之前设置!
        httpExchange.getResponseHeaders().add("Content-Type:application/x-www-form-urlencoded", "text/html;charset=utf-8");
        //设置响应码和响应体长度,必须在getResponseBody方法之前调用!
        httpExchange.sendResponseHeaders(200, responseContentByte.length);
        OutputStream out = httpExchange.getResponseBody();
        out.write(responseContentByte);
        out.flush();
        out.close();
    }
}


  • 写回答

4条回答 默认 最新

  • 普通网友 2021-09-23 09:33
    关注

    一般来说,不会有区别;

    如果你服务器上不能操作;
    那么,你需要检查服务器;
    1、比如防火墙有没有放开;

    2、服务有没有正常启动

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

报告相同问题?

问题事件

  • 系统已结题 10月1日
  • 已采纳回答 9月23日
  • 创建了问题 9月23日

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作