java - Trying to HTTP POST But Getting MalformedURLException: no protocol: yahoo.com -


overall i'm trying write script captures servers' response http post using java.

unfortunately, i'm stuck @ encoding url portion of it. while followed several online example on encoding url, still malformedurlexception...

any idea might go wrong in encoding process?

the error:

$ java client_post  sending http post request exception in thread "main thread" java.net.malformedurlexception: no  protocol: http%3a%2f%2fyahoo.com      @ java.net.url.<init>(url.java:567)      @ java.net.url.<init>(url.java:465)      @ java.net.url.<init>(url.java:414)      @ client_post.sendpost(client_post.java:30)      @ client_post.main(client_post.java:23) 

the code:

//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 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());         }  } 

when encoding url url becomes below

http%3a%2f%2fyahoo.com

dont encode untill have special in it.

your programm throwing class cast exception

 httpsurlconnection con = (httpsurlconnection) obj.openconnection(); 

above should below

 httpurlconnection con = (httpurlconnection) obj.openconnection(); 

below working programm.

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 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());         }  } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -