Java的:如何使用URLConnection的发布与授权请求? [英] Java: how to use UrlConnection to post request with authorization?

查看:138
本文介绍了Java的:如何使用URLConnection的发布与授权请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成POST请求,要求身份验证的服务器。我试着用下面的方法:

I would like to generate POST request to a server which requires authentication. I tried to use the following method:

private synchronized String CreateNewProductPOST (String urlString, String encodedString, String title, String content, Double price, String tags) {

    String data = "product[title]=" + URLEncoder.encode(title) +
                "&product[content]=" + URLEncoder.encode(content) + 
                "&product[price]=" + URLEncoder.encode(price.toString()) +
                "&tags=" + tags;
    try {
        URL url = new URL(urlString);
        URLConnection conn;
        conn = url.openConnection();
        conn.setRequestProperty ("Authorization", "Basic " + encodedString);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush(); 
        // Get the response 
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
        String line; 
        while ((line = rd.readLine()) != null) { 
            // Process line... 
            } 
        wr.close(); 
        rd.close(); 
        return rd.toString();
    } catch (MalformedURLException e) {

        e.printStackTrace();
        return e.getMessage();
    }
    catch (IOException e) {

        e.printStackTrace();
        return e.getMessage();
    } 
}

但服务器不接收授权数据。这是为了增加授权数据行是:

but the server doesn't receive the authorization data. The line which is supposed to add authorization data is the following:

conn.setRequestProperty ("Authorization", "Basic " + encodedString);

和行

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

还抛出IOException。

also throws an IOException.

无论如何,我会非常感激,如果任何人都可以提出的上方,以便用POST用的URLConnection,以使授权逻辑的任何修补程序。

Anyway I would be very thankful if anyone could suggest any fix of the logic above in order to enable authorization using POST with UrlConnection.

但显然,它应该是但如果同样的逻辑用于GET请求一切正常,这是行不通的。

but obviously it doesn't work as it is supposed to although if the same logic is used for GET request everything works fine.

推荐答案

一个精细例如这里发现。 Powerlord 得到它的权利,下面 ,对于POST需要<一个href=\"http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html\"><$c$c>HttpURLConnection,来代替。

A fine example found here. Powerlord got it right, below, for POST you need HttpURLConnection, instead.

下面是code要做到这一点,

Below is the code to do that,

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestProperty ("Authorization", encodedCredentials);

    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

    writer.write(data);
    writer.flush();
    String line;
    BufferedReader reader = new BufferedReader(new 
                                     InputStreamReader(conn.getInputStream()));
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }
    writer.close();
    reader.close();

修改的URLConnection HttpURLConnection类,使它POST请求。

Change URLConnection to HttpURLConnection, to make it POST request.

    HttpURLConnection conn = url.openConnection();
    conn.setRequestMethod("POST");

建议(......在评论):

您可能需要太设置这些属性,

You might need to set these properties too,

conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "Accept", "*/*" );

这篇关于Java的:如何使用URLConnection的发布与授权请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆