zoukankan      html  css  js  c++  java
  • 远程访问

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(baseUrl + action);
    HttpResponse response = httpclient.execute(httpget);
    retStr = EntityUtils.toString(response.getEntity());

    JSONObject objRoot = new JSONObject(retStr );
    retStr = objRoot.getString("qwe");

    所用到的jar 包:httpcore httpclient
    http://www.cnblogs.com/Wen-yu-jing/p/3552154.html

    public static String sendPost(String host,String content,String encodeType) throws UnsupportedEncodingException,IOException
    {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost post= new HttpPost(host);

    //NameValuePair p = new BasicNameValuePair("v", "123");
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    list.add(new BasicNameValuePair("v", "1405322466697"));
    StringEntity entity = new UrlEncodedFormEntity(list);
    post.setEntity(entity);


    StringEntity myEntity;
    if(!encodeType.isEmpty())
    myEntity = new StringEntity(content,encodeType);
    else
    myEntity = new StringEntity(content);
    post.addHeader("Content-Type", "text/xml");
    post.addHeader("Connection","keep-alive");
    post.addHeader("Accept","*/*");
    post.addHeader("Cache-Control", "max-age=0");
    post.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
    post.setEntity(myEntity);
    HttpResponse response = httpClient.execute(post);
    String answerRes = "";
    if(response.getStatusLine().getStatusCode()==200)
    {
    //如果状态码为200,就是正常返回
    String result=EntityUtils.toString(response.getEntity());
    answerRes = new String(result.getBytes("iso-8859-1"), "UTF-8");
    }
    return answerRes;
    }

    public static String sendGet(String url) throws UnsupportedEncodingException,IOException
    {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet get= new HttpGet(url);
    get.addHeader("Connection","keep-alive");
    get.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8");
    get.addHeader("Cache-Control", "max-age=0");
    get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
    HttpResponse response = httpClient.execute(get);
    String answerRes = "";
    if(response.getStatusLine().getStatusCode()==200)
    {
    //如果状态码为200,就是正常返回
    String result=EntityUtils.toString(response.getEntity());
    answerRes = new String(result.getBytes("iso-8859-1"), "UTF-8");
    }
    return answerRes;
    }

  • 相关阅读:
    poj 3304 Segments 直线 线段求交
    poj 1077 Eight 八数码 A*算法
    UESTC 1447 Area 凸包+旋转卡壳 求最大四边形面积
    ACM计算几何题目推荐(第二期)
    poj 2398 Toy Storage 叉乘
    ACM计算几何题目推荐 (第一期)
    (转载)Telnet协议详解及使用C# 用Socket 编程来实现Telnet协议
    jquery 表情编辑器
    (读书笔记)Asp.net Mvc 与WebForm 混合开发
    (转载)精简说明C#最基本的Socket编程示例
  • 原文地址:https://www.cnblogs.com/Wen-yu-jing/p/3767233.html
Copyright © 2011-2022 走看看