使用HttpUrlConnection发布数据Java [英] Post data Java with HttpUrlConnection

查看:126
本文介绍了使用HttpUrlConnection发布数据Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过他们的Java API将文件上传到Rapidshare.但是我被困住了,我想我的问题是他们的API不知道我上传的文件的属性名称,或者类似的名称.看来它可以完美地上传和提交,但是此后我的帐户中没有文件.

I'm trying to upload a file to Rapidshare through their API with Java. But I'm stuck and I think my problem is that their API don't know the property name of my uploaded file, or something like that. It seems like it uploads and submits perfectly, but no files are in my account afterwards.

我的代码基于在这里找到的内容: http: //www.experts-exchange.com/Programming/Languages/Java/Q_20370019.html

I've based my code on what I found here: http://www.experts-exchange.com/Programming/Languages/Java/Q_20370019.html

如果有帮助,我成功使用PHP和cURL创建了一个解决方案,但最终的解决方案必须是Java.

If any help, I succeeded to create a solution using PHP and cURL, but the final solution must be in Java.

$post = array(
  'sub'         => 'upload',
  'login'       => $user,
  'password'    => $pass,
  'filecontent' => '@'. $filepath
);

$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, 'https://rs'. $server .'.rapidshare.com/cgi-bin/rsapi.cgi');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $post);
$info = curl_exec($c);
curl_close($c);

我的Java来源:

package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void main(String args[]) throws Exception {
        String url = "http://rs"+ getServerId() +".rapidshare.com/cgi-bin/rsapi.cgi?sub=upload&login=***&password=***";
        String docPath = "/Users/***/Downloads/a.jpg";    // Document 

        HttpURLConnection httpcon = (HttpURLConnection) ((new URL(url).openConnection()));
        httpcon.setDoOutput(true);
        httpcon.setUseCaches(false);
        httpcon.setRequestProperty("Content-Type", "multipart/form-data");
        httpcon.connect();

        System.out.println("Posting " +docPath +"...");
        File file = new File(docPath);
        FileInputStream is = new FileInputStream(file);
        OutputStream os = httpcon.getOutputStream();

        // POST
        // Read bytes until EOF to write
        byte[] buffer = new byte[4096];
        int bytes_read;    // How many bytes in buffer
        while((bytes_read = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytes_read);
        }
        os.close();
        is.close();

        System.out.println("Done...");
    }

    private static int getServerId() throws NumberFormatException, IOException {
        HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL(
                "http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver")
                .openConnection();
        httpUrlConnection.connect();
        InputStreamReader in = new InputStreamReader(
                (InputStream) httpUrlConnection.getContent());

        BufferedReader buff = new BufferedReader(in);

        return Integer.parseInt(buff.readLine());
    }
}

推荐答案

我在我的项目中使用了以下类. http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

I used the following class for my project. http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

它支持GET和POST请求.

It supports GET and POST requests.

您可以像

RestClient client = new RestClient(your_url);
client.AddParam("sub", "upload");
//Other parameters here

try { 
    client.Execute(RequestMethod.POST); 
} catch (Exception e) {} 

这篇关于使用HttpUrlConnection发布数据Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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