2

次のコードを使用してGoogleにデータを投稿しようとしましたが、常に405エラーが発生しました。誰か教えてもらえますか?

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

1 に答える 1

6

405 は「メソッドが許可されていません」を意味します。たとえば、POST許可されていない URL にアクセスしようとするとPOST、サーバーは 405 ステータスを返します。

Google に POST リクエストを送信して何をしようとしていますか? Google のホームページでは、GET、HEAD、およびおそらく OPTIONS のみが許可されているのではないかと思います。

これは、Google の説明を含む、Google への POST リクエストの本文です。

405.それはエラーです。

URL に対してリクエスト メソッドPOSTが不適切です/。それが私たちが知っているすべてです。

于 2012-05-07T04:36:16.807 に答える