操作HTTP协议发送数据

客户端部分代码:
public String requestV8Api(String params, String mothodName)
{
String result = null;
String errcode = "";
HttpURLConnection conn = null;
String apiUrl = (new StringBuilder(String.valueOf(Configuration.getString("apiURL")))).append("/v8ApiServer").append("/v8ApiServer").toString();
URL url = ApiUtil.getUrl(apiUrl);
try
{
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(1000000);
conn.setReadTimeout(1000000);
conn.setDoOutput(true);
/**如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。**/
conn.setDoInput(true);
/**如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。**/
/**在请求的header中写入信息 start**/
conn.setRequestMethod("POST");
conn.setRequestProperty("mothodName", mothodName);
conn.setRequestProperty("Content-type", "application/octest-stream");
conn.setRequestProperty("apiversion", "1.0");
String publickey = Configuration.getString("publickey");
conn.setRequestProperty("publickey", publickey);
conn.setRequestProperty("privatekey", "v8Online");
String appid = Configuration.getString("appid");
conn.setRequestProperty("appid", appid);
/**在请求的header中写入信息 end**/

OutputStream out = conn.getOutputStream();
/**返回写入到此连接的输出流**/
ObjectOutputStream objStream = new ObjectOutputStream(out);
objStream.writeObject(params);
/**用于往请求中写入数据发送给服务器端,服务器端通过getInputStream().readObject()方法得到客户端输入的数据流*/
objStream.flush();
objStream.close();
out.close();
conn.getOutputStream().flush();
conn.getOutputStream().close();

InputStream in = conn.getInputStream();
/**返回从此打开的连接读取的输入流**/
int responseCode = conn.getResponseCode();
if(200 != responseCode)
{
errcode = "4";
System.out.print((new StringBuilder("\u670D\u52A1\u5668 HTTP \u72B6\u6001\u7801\uFF1A")).append(responseCode).toString());
result = (new StringBuilder("result:{errcode:")).append(errcode).append("}").toString();
}
ObjectInputStream back = new ObjectInputStream(in);
result = (String)back.readObject();
/**用于从读取服务器端通过getOutputStream().writeObject()方法写入response中的数据流**/
back.close();
in.close();
}
catch(IOException e)
{
errcode = "1";
result = (new StringBuilder("result:{errcode:")).append(errcode).append("}").toString();
}
catch(ClassNotFoundException e)
{
errcode = "1";
result = (new StringBuilder("result:{errcode:")).append(errcode).append("}").toString();
}
return result;
}


服务器端部分代码:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(req.getInputStream()));
/**用于从获得从客户端通过getOutputStream().writeObject()方法写入的数据流**/

Log log = LogFactory.getLog(getClass());
String result = null;
try
{
String params = (String)in.readObject();
/**用于从获得从客户端通过getOutputStream().writeObject()方法写入的数据流**/
System.out.println(params);
CallMothodController controller = new CallMothodController();
Map paramsMap = controller.params(params);

SYSParamBean bean = new SYSParamBean();
bean.setApi_version(req.getHeader("apiversion"));
bean.setApp_id(req.getHeader("appid"));
bean.setMothodName(req.getHeader("mothodName"));

log(req.getHeader("mothodName"));
bean.setPrivate_key(req.getHeader("privatekey"));
bean.setPublic_key(req.getHeader("publickey"));

String errCode = controller.checkJurisdiction(bean);
if ("".equals(errCode)) {
result = controller.getMothodObject(bean.getMothodName(), paramsMap);
in.close();

out = new ObjectOutputStream(resp.getOutputStream());
out.flush();

out.writeObject("result:{content:" + result + ",errcode:[]" + "}");
out.close(); return;
}
in.close();

ObjectOutputStream out = new ObjectOutputStream(resp.getOutputStream());
/**打开response的输出流**/
out.flush();

out.writeObject("result:{errcode:" + errCode + "}");
/**向响应response中写入数据**/
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

你可能感兴趣的:(操作HTTP协议发送数据)