java.lang.RuntimeException:失败:HTTP错误代码:404

时间:2014-09-12 15:58:36

标签: docusignapi

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class NetClientGet {

    public static void main(String[] args) {

      try {

        URL url = new URL("https://demo.docusign.net/restapi/v2/login_information");
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.ops.tiaa-cref.org", 8080));
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(proxy);
        conn.setRequestMethod("GET");

        conn.setRequestProperty("content-type", "application/json");
        conn.setRequestProperty("Username", "puviars@gmail.com");
        conn.setRequestProperty("Password", "*******");
        conn.setRequestProperty("IntegratorKey", "TIAC-e30cd896-cd8b-4cca-8551-86b8c51ea85a");
        //conn.setRequestProperty("X-DocuSign-Authentication","{\"Username\":\"puviars@gmail.com\",\"Password\":\"********\",\"IntegratorKey\":\"TIAC-e30cd896-cd8b-4cca-8551-86b8c51ea85a\"}");

        //String input = "{\"api_password\":\"false\",\"include_account_id_guid\":\"false\",\"login_settings\":\"none\"}";

        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("api_password", "true"));
        params.add(new BasicNameValuePair("include_account_id_guid", "false"));
        params.add(new BasicNameValuePair("login_settings", "all"));

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getQuery(params));
        writer.flush();
        writer.close();
        os.close();

        //conn.setDoOutput(true);
        //OutputStream os = conn.getOutputStream();
        //os.write(input.getBytes());
        //os.flush();


        //conn.setRequestProperty("api_password","false");
       //conn.setRequestProperty("include_account_id_guid","false");
        //conn.setRequestProperty("login_settings","none");

    if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {

            System.out.println(output);
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

      }

    }

    private static String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
    {
        StringBuilder result = new StringBuilder();
        boolean first = true;

        for (NameValuePair pair : params)
        {
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
        }

        return result.toString();
    }

}

//

在通过httpsurlconnection调用rest url时,eclispe IDE中的以下代码使用run as java program.i得到以下错误:

线程“main”中的异常java.lang.RuntimeException:失败:HTTP错误代码:404     在NetClientGet.main(NetClientGet.java:71)

请帮我解决一下。这段代码只是为了点击demo url登录&amp;获取详细信息&amp;然后我将继续发送pdf文件来设计它。但如果我使用他们的互动网站,我可以得到JSON的回复。

//

0 个答案:

没有答案