Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7902941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:38:11+00:00 2026-06-03T09:38:11+00:00

I just try to post data to google by using the following code,but always

  • 0

I just try to post data to google by using the following code,but always got 405 error,can anybody tell me way?

package com.tom.labs;
import java.net.*;
import java.io.*;
public class JavaHttp {
    public static void main(String[] args) throws Exception {
        File data = new File("D:\\in.txt");
        File result = new File("D:\\out.txt");
        FileOutputStream out = new FileOutputStream(result);
        OutputStreamWriter writer = new OutputStreamWriter(out); 
        Reader reader = new InputStreamReader(new FileInputStream(data));
        postData(reader,new URL("http://google.com"),writer);//Not working
        //postData(reader,new URL("http://google.com/search"),writer);//Not working
        sendGetRequest("http://google.com/search", "q=Hello");//Works properly
    }

    public static String sendGetRequest(String endpoint,
            String requestParameters) {
        String result = null;
        if (endpoint.startsWith("http://")) {
            // Send a GET request to the servlet
            try {
                // Send data
                String urlStr = endpoint;
                if (requestParameters != null && requestParameters.length() > 0) {
                    urlStr += "?" + requestParameters;
                }
                URL url = new URL(urlStr);
                URLConnection conn = url.openConnection();
                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(
                        conn.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = rd.readLine()) != null) {
                    sb.append(line);
                }
                rd.close();
                result = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(result);
        return result;
    }

    /**
     * Reads data from the data reader and posts it to a server via POST
     * request. data - The data you want to send endpoint - The server's address
     * output - writes the server's response to output
     * 
     * @throws Exception
     */
    public static void postData(Reader data, URL endpoint, Writer output)
            throws Exception {
        HttpURLConnection urlc = null;
        try {
            urlc = (HttpURLConnection) endpoint.openConnection();
            try {
                urlc.setRequestMethod("POST");
            } catch (ProtocolException e) {
                throw new Exception(
                        "Shouldn't happen: HttpURLConnection doesn't support POST??",
                        e);
            }
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            urlc.setUseCaches(false);
            urlc.setAllowUserInteraction(false);
            urlc.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
            OutputStream out = urlc.getOutputStream();
            try {
                Writer writer = new OutputStreamWriter(out, "UTF-8");
                pipe(data, writer);
                writer.close();
            } catch (IOException e) {
                throw new Exception("IOException while posting data", e);
            } finally {
                if (out != null)
                    out.close();
            }
            InputStream in = urlc.getInputStream();
            try {
                Reader reader = new InputStreamReader(in);
                pipe(reader, output);
                reader.close();
            } catch (IOException e) {
                throw new Exception("IOException while reading response", e);
            } finally {
                if (in != null)
                    in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("Connection error (is server running at "
                    + endpoint + " ?): " + e);
        } finally {
            if (urlc != null)
                urlc.disconnect();
        }
    }

    /**
     * Pipes everything from the reader to the writer via a buffer
     */
    private static void pipe(Reader reader, Writer writer) throws IOException {
        char[] buf = new char[1024];
        int read = 0;
        while ((read = reader.read(buf)) >= 0) {
            writer.write(buf, 0, read);
        }
        writer.flush();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:38:12+00:00Added an answer on June 3, 2026 at 9:38 am

    405 means “method not allowed”. For example, if you try to POST to a URL that doesn’t allow POST, then the server will return a 405 status.

    What are you trying to do by making a POST request to Google? I suspect that Google’s home page only allows GET, HEAD, and maybe OPTIONS.

    Here’s the body of a POST request to Google, containing Google’s explanation.

    405. That’s an error.

    The request method POST is inappropriate for the URL /. That’s all we know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I try to post data values in PHP I get the following error:
i use below code - Just try again. - to prevent deadlock. it seems
I am using ajaxSubmitButton to POST data to my controller. That controller action then
I am using urllib2 to post data to a form. The problem is that
I have a problem and after 2 hours of try & error & google
I have the following form working just fine in Chrome, but doesn't seem to
I used mmap(just try to understand how mmap works) to allocate 96k anonymous memory,
Is there a way to tell ifort or gfortran to just try to parse
I came across an issue where, just to try it out, I removed an
I tried setting all possible styles to something other than grey, just to try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.