java - 尝试 HTTP POST 但收到 MalformedURLException : no protocol: yahoo. com

标签 java http-post

总的来说,我正在尝试编写一个脚本来捕获服务器对使用 java 的 HTTP POST 的响应。

不幸的是,我一直在对它的 URL 部分进行编码。虽然我遵循了几个关于编码 URL 的在线示例,但我仍然得到 MalformedURLException...

知道编码过程中可能出现什么问题吗?

错误:

$ java client_post

Sending Http POST request
Exception in thread "Main Thread" java.net.MalformedURLException: no 
protocol: http%3A%2F%2Fyahoo.com
     at java.net.URL.<init>(URL.java:567)
     at java.net.URL.<init>(URL.java:465)
     at java.net.URL.<init>(URL.java:414)
     at client_post.sendPost(client_post.java:30)
     at client_post.main(client_post.java:23)

代码:

//package client_post;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class client_post {

     private final String USER_AGENT = "Mozilla/5.0";

     public static void main(String[] args) throws Exception {

             client_post http = new client_post();

             System.out.println("\nSending Http POST request");
             http.sendPost();

     }
     // HTTP POST request
     private void sendPost() throws Exception {

             //String url =<host:port/create/service>
             String url = "http://yahoo.com";
             String EncoderUrl = URLEncoder.encode(url, "UTF-8");
             URL obj = new URL(EncoderUrl);
             HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

             //add reuqest header
             con.setRequestMethod("POST");
             con.setRequestProperty("User-Agent", USER_AGENT);
             con.setRequestProperty("Accept-Language","en-US,en;q=0.5");

             String urlParameters = "<string base64>";
// Send post request
             con.setDoOutput(true);
             DataOutputStream wr = new DataOutputStream(con.getOutputStream());
             wr.writeBytes(urlParameters);
             wr.flush();
             wr.close();

             int responseCode = con.getResponseCode();
             System.out.println("\nSending 'POST' request to URL : " + url);
             System.out.println("Post parameters : " + urlParameters);
             System.out.println("Response Code : " + responseCode);

             BufferedReader in = new BufferedReader(
                     new InputStreamReader(con.getInputStream()));
             String inputLine;
             StringBuffer response = new StringBuffer();

             while ((inputLine = in.readLine()) != null) {
                     response.append(inputLine);
             }
             in.close();

             //print result
             System.out.println(response.toString());
            System.out.println(response.toString());

       }

}

最佳答案

当你对 url 进行编码时,你的 url 会变成如下所示

http%3A%2F%2Fyahoo.com

不要编码,直到你有一些特别的东西。

你的程序也抛出类转换异常

 HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

上面应该像下面这样

 HttpURLConnection con = (HttpURLConnection) obj.openConnection();

下面是工作程序。

package com.ds.portlet.library;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;


public class client_post {

     private final String USER_AGENT = "Mozilla/5.0";

     public static void main(String[] args) throws Exception {

             client_post http = new client_post();

             System.out.println("\nSending Http POST request");
             http.sendPost();

     }
     // HTTP POST request
     private void sendPost() throws Exception {

             //String url =<host:port/create/service>
             String url = "http://yahoo.com";
//             String EncoderUrl = URLEncoder.encode(url, "UTF-8");
             URL obj = new URL(url);
             HttpURLConnection con = (HttpURLConnection) obj.openConnection();

             //add reuqest header
             con.setRequestMethod("POST");
             con.setRequestProperty("User-Agent", USER_AGENT);
             con.setRequestProperty("Accept-Language","en-US,en;q=0.5");

             String urlParameters = "<string base64>";
// Send post request
             con.setDoOutput(true);
             DataOutputStream wr = new DataOutputStream(con.getOutputStream());
             wr.writeBytes(urlParameters);
             wr.flush();
             wr.close();

             int responseCode = con.getResponseCode();
             System.out.println("\nSending 'POST' request to URL : " + url);
             System.out.println("Post parameters : " + urlParameters);
             System.out.println("Response Code : " + responseCode);

             BufferedReader in = new BufferedReader(
                     new InputStreamReader(con.getInputStream()));
             String inputLine;
             StringBuffer response = new StringBuffer();

             while ((inputLine = in.readLine()) != null) {
                     response.append(inputLine);
             }
             in.close();

             //print result
             System.out.println(response.toString());
            System.out.println(response.toString());

       }

}

关于java - 尝试 HTTP POST 但收到 MalformedURLException : no protocol: yahoo. com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30253225/

相关文章:

java - 在 JBOSS 7 中部署 Struts 2 项目

java - 使用基于表单的容器管理安全性重试登录

ruby - ruby 中等效的 curl 命令

php - 安卓 : Upload image to PHP server

php - 自定义二进制数据的 cURL POST(不是表单内容)

android - HTTPPost 上的 "417: Expectation Failed"

java - 了解 Java 中的类型删除

java - 任务计划和 DST 更改

Android,为什么 HttpPost 方法在 Android v2.3 和 Android v4.0.3 中返回两个不同的结果?

java - Jax-WS Glassfish XSD : ? xsd=1 与 ?xsd=2