dongshendie8849 2014-10-01 06:51
浏览 23
已采纳

使用java进行卷曲操作[关闭]

I have done a curl operation using php. This is my code.

$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic ' . base64_encode($username . ":" . $password)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonObjectFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);

Now I need to rewrite this code in java. I am not much familler with HttpURLConnection in java. Can any body help?

  • 写回答

1条回答 默认 最新

  • dongtui4038 2014-10-01 07:02
    关注

    Yes you can use HttpURLConnection

    HttpURLConnection myConnection = (HttpURLConnection) ((new URL("myurl").openConnection()));
    myConnection.setDoOutput(true);
    myConnection.setRequestProperty("Content-Type", "application/json");
    myConnection.setRequestProperty("Accept", "application/json");
    myConnection.setRequestMethod("POST");
    myConnection.connect();
    
    String jsonData = "{'foo' : 'bar'}";
    byte[] outputBytes = jsonData.getBytes("UTF-8");
    OutputStream os = myConnection.getOutputStream();
    os.write(outputBytes);
    os.close();
    

    I hope my answer is clear enough. If you have any question regarding my answer you can ask in the comment :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度