码迷,mamicode.com
首页 > Web开发 > 详细

客服端与服务器端JSON数据交互的POST传输:

时间:2016-06-29 12:50:59      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

post请求客户端端代码:
1.
public void String LoginByPost(String username,String pwd){
     String path = "";
     URL url = new URL(paht);
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     conn.setConnectTimeout(5000);//请求超时
     conn.setRequestMehtod("POST");

//准备数据
    String data ="username=" + username +"&pwd=" + pwd;
    conn.setRequestProperty("Content-Type","application/x-form-urlencoded");
    conn.setRequestProperty("Content-Length", data.length + "");

    conn.setDoOutput(true); //是否允许向外写数据
    OputStream os = conn.getOutputStream();
    os.write(data.getByte());

    int code = conn.getResponseCode();
    if(code == 200){
         InputStream is = conn.getInputStream();
         String text = StreamTool.readInputStream(is);
         return text;
     }else{
        return null;
}
}

2.
public void String LoginClientPost(String username, String pwd){
      HttpClient client = new DefultHttpClient();
      String path = "";
      HttpPost = post = new HttpPost(path);
      List<NameValuePair> paimer = new ArrayList<NameValuePair>();
      paimer.add(new BasicNameValuePair("username",username));
      post.setEntity(new UrlEncodedFormEntiry(paimer,"UTF-8"));

      HttpResponse response = client.excute(post);
      int code = response.getStatusLine().getStatusCode();
      if(code == 200){
           InputStream is = response.getEntity().getContent();
           String text = StreamTool.readStream(is);
           return text;
      }else{
           return null;
      }

}

客服端与服务器端JSON数据交互的POST传输:

标签:

原文地址:http://www.cnblogs.com/the-boy/p/5626296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!