方法调用:

//测试

public static void main(String[] args) {

  Map map = new HashMap();

  map.put("type", "update" );

  map.put("loginName", "4" );

  map.put("userpasswd", "96e79218965eb72c92a549dd5a330112" );

  map.put("email", "4444@qq.com" );

  JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);

  String strJsonObject=jsonObject.toString();

  String url = "http://localhost:8080/xtmc/synchroUser/insertWebUser.do?userData="+strJsonObject;

  System.out.println("注册同步 "+url);

  String jsonString = HttpRequestUtil.sendPost(url);

  System.out.println("返回的结果是"+jsonString);

}

sendPost(url)方法见链接:

http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html

或者:

/**
* 向指定 URL 发送POST方法的请求
*
* @param pathUrl 发送请求的 URL
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String pathUrl) {
try {
  URL url = new URL(pathUrl);
  HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();   // //设置连接属性
  httpConn.setDoOutput(true);// 使用 URL 连接进行输出
  httpConn.setDoInput(true);// 使用 URL 连接进行输入
  httpConn.setUseCaches(false);// 忽略缓存
  httpConn.setRequestMethod("POST");// 设置URL请求方法
  String requestString = "客服端要以以流方式发送到服务端的数据...";   // 设置请求属性
  // 获得数据字节数据,请求数据流的编码,必须和下面服务器端处理请求流的编码一致
  byte[] requestStringBytes = requestString.getBytes("utf-8");
  httpConn.setRequestProperty("Content-length", "" + requestStringBytes.length);
  httpConn.setRequestProperty("Content-Type", "application/octet-stream");
  httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
  httpConn.setRequestProperty("Charset", "UTF-8");
  //
  String name = URLEncoder.encode("黄武艺", "utf-8");
  httpConn.setRequestProperty("NAME", name);   // 建立输出流,并写入数据
  OutputStream outputStream = httpConn.getOutputStream();
  outputStream.write(requestStringBytes);
  outputStream.close();
  // 获得响应状态
  int responseCode = httpConn.getResponseCode();   if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功
    // 当正确响应时处理数据
    StringBuffer sb = new StringBuffer();
    String readLine;
    BufferedReader responseReader;
    // 处理响应流,必须与服务器响应流输出的编码一致
    responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "utf-8"));     while ((readLine = responseReader.readLine()) != null) {
      sb.append(readLine).append("\n");
    }
    responseReader.close();
    System.out.println(sb.toString());
    return sb.toString();
  }
} catch (Exception e) {
  System.out.println(e.getStackTrace());
}
return null;
}

被请求方法(/synchroUser/insertWebUser.do ):

@RequestMapping("/synchroUser/insertWebUser.do")
public void insertWebUser(HttpServletResponse response,HttpServletRequest request,@RequestParam("userData") String userData) throws Exception {
  JSONObject dataMap = null;
  System.out.println("接收到的userData==="+userData);
  try {
    dataMap = JSONObject.parseObject(userData);
  } catch (Exception e) {
    System.out.println("参数解析错误!");
    e.printStackTrace();
  }   String loginName = getJsonValue(dataMap, "loginName");
  String userpasswd = getJsonValue(dataMap, "userpasswd");
  String email = getJsonValue(dataMap, "email");
  String type = getJsonValue(dataMap, "type"); } catch (Exception e) {
  // TODO: handle exception
  flag = "false";
  e.printStackTrace();
}
  response.getWriter().write("success");
} /**
* 获取json对象中的属性
* @param params json对象
* @param key 参数名
* @return
*/
private String getJsonValue(JSONObject params, String key) {
  if (params == null || "".equals(key) || key == null) {
    return null;
  }
  Object obj = params.get(key);
  return obj == null ? null : obj.toString();
}

HttpURLConnection请求的更多相关文章

  1. HttpURLConnection请求数据流的写入(write)和读取(read)

    URLConnection类给应用 程序 和web资源之间架设起了通信的桥梁,这些web资源通常是通过url来标记的,本文将讲述如何使用HttpURLConnection来访问web页面(发送数据流) ...

  2. Android使用HttpUrlConnection请求服务器发送数据详解

    HttpUrlConnection是java内置的api,在java.net包下,那么,它请求网络同样也有get请求和post请求两种方式.最常用的Http请求无非是get和post,get请求可以获 ...

  3. 解决Fiddler不能监听Java HttpURLConnection请求的方法

    在默认情况下,Fiddler不能监听Java HttpURLConnection请求.究其原因,Java的网络通信协议栈可能浏览器的通信协议栈略有区别,Fiddler监听Http请求的原理是 在应用程 ...

  4. 使用Fiddler监听java HttpURLConnection请求

    使用Fiddler监听java HttpURLConnection请求

  5. 分享自己配置的HttpURLConnection请求数据工具类

    >>该工具类传入string类型url返回string类型获取结果import java.io.BufferedReader;import java.io.InputStream;impo ...

  6. HttpURLConnection请求网络数据

    //使用线程 new Thread(){            public void run() {                try {                    //先创建出了一 ...

  7. HttpURLConnection请求接口

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; ...

  8. 使用HttpURLConnection请求multipart/form-data类型的form提交

    写一个小程序,模拟Http POST请求来从网站中获取数据.使用Jsoup(http://jsoup.org/)来解析HTML. Jsoup封装了HttpConnection的功能,可以向服务器提交请 ...

  9. HttpURLConnection请求网络数据的Post请求

    //--------全局变量----------- //注册Url    private String urlPath="http://101.200.142.201:8080/VideoP ...

随机推荐

  1. (转)STORM启动与部署TOPOLOGY

    STORM启动与部署TOPOLOGY 启动ZOOPKEEPER zkServer.sh start 启动NIMBUS storm nimbus & 启动SUPERVISOR storm sup ...

  2. 页面显示(pageshow)和页面隐藏(pagehide)事件

    Firefox和Opera有一个新特性,名叫“往返缓存”(back-forward cache,或bfcache),可以在用户使用浏览器的“后退”和“前进”按钮时加快页面的转换速度.这个缓存中不仅保存 ...

  3. POJ1419 & 最大团

    题意: 求一个图的最大点独立集.SOL: 转化为补图的最大团,最大团似乎是一个NP问题,那么只好爆搜了. 补一补图论基础,代码不想打了,来自某blog #include <iostream> ...

  4. CentOS下强行umount卸载设备

    fuser -cu /usr/local/tomcat7/webapps/dsideal_yy/html/down/ fuser -ck /usr/local/tomcat7/webapps/dsid ...

  5. HDU-I Hate It

    Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写 ...

  6. 转:jQuery插件开发精品教程,让你的jQuery提升一个台阶

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  7. 【ZOJ】3329 One Person Game

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个色子,分别有k1.k2.k3个面,权值分别是1-k1, 1~ ...

  8. HDU 4749 Parade Show(贪心+kmp)

    题目链接 题目都看不懂,做毛线...看懂了之后就是kmp出,所有的匹配区间,然后DP可以写,贪心也可以做把,DP应该需要优化一下,直接贪,也应该对的,经典贪心问题. #include<iostr ...

  9. wind.onload和$(document).ready()的区别例示

    例子: <html> <script type="text/javascript" src="jquery-1.7.1.min.js"> ...

  10. 在DataGridView控件中加入ComboBox下拉列表框的实现

    在DataGridView控件中加入ComboBox下拉列表框的实现 转自:http://www.cnblogs.com/luqingfei/archive/2007/03/28/691372.htm ...