方法调用:

//测试

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. 经典收藏 50个jQuery Mobile开发技巧集萃

    http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 1.Backbone移动实例 这是在Safari中运行的一款Ba ...

  2. js判断访问来源

    通过navigator的userAgent属性来判定 userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值.一般来讲,它是在 navigator.appCode ...

  3. [WP8.1UI控件编程]Windows Phone理解和运用ItemTemplate、ContentTemplate和DataTemplate

    2.2.5 ItemTemplate.ContentTemplate和DataTemplate 在理解ItemTemplate.ContentTemplate和DataTemplate的关系的之前,我 ...

  4. [Cocos2D-x For WP8]ParallaxNode视差

    视差就是从有一定距离的两个点上观察同一个目标所产生的方向差异.从目标看两个点之间的夹角,叫做这两个点的视差角,两点之间的距离称作基线.只要知道视差角度和基线长度,就可以计算出目标和观测者之间的距离.游 ...

  5. nodejs入门(一)

    1.nodejs简介  Nodejs不是一个js应用.而是一个js运行平台. Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效 2. 1).Nodejs内置了一个HTTP模块 var ...

  6. 【Go语言】错误与异常处理机制

    ①error接口 Go语言中的error类型实际上是抽象了Error()方法的error接口 type error interface { Error() string } Go语言使用该接口进行标准 ...

  7. IDictionary<TKey, TValue> vs. IDictionary

    Enumerating directly over an IDictionary<TKey,TValue>returns a sequence of  KeyValuePair struc ...

  8. mysql安装及卸载

    一.关于mysql MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是 ...

  9. Struts2中重定向和请求转发配置

    struts2中默认跳转为dispatcher请求转发 只能往jsp转发,跳转action报404 重定向 设置为redirect ,可以是jsp也可以是action <!--同一个包下的act ...

  10. js添加确认删除操作注意事项

    function delsure(){ if(confirm('确认删除吗?')){ return true;//点击确定则返回这里的内容 }else{ return false; } } 在表单中添 ...