如何将参数传递给uploadphoto任务方法?

时间:2014-12-15 10:35:26

标签: java android .net asp.net-mvc asp.net-web-api

我有一个可以上传照片的方法:

 public class uploadphotos : ApiController
    {
        public Task<HttpResponseMessage> Post( )
        {

            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            string root =HostingEnvironment.MapPath("~/photos");//Burdaki app data klasoru degisecek
            var provider = new MultipartFormDataStreamProvider(root);

            // Read the form data and return an async task.
            var task = Request.Content.ReadAsMultipartAsync(provider).
                ContinueWith<HttpResponseMessage>(t =>
                {
                    if (t.IsFaulted || t.IsCanceled)
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
                    }

                    // This illustrates how to get the file names.
                    foreach (MultipartFileData file in provider.FileData)
                    {

                        string fileName = file.LocalFileName;
                        string originalName = file.Headers.ContentDisposition.FileName;


                        FileInfo file2 = new FileInfo(fileName);
                        file2.CopyTo(Path.Combine(root, originalName.TrimStart('"').TrimEnd('"')), true);
                        file2.Delete();

                        //Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                        // Trace.WriteLine("Server file path: " + file.LocalFileName);
                    }
                    return Request.CreateResponse(HttpStatusCode.OK);
                });


            return task;
        }

    }

工作正常。但我想将参数发送到该方法,我该怎么做?

我应该这样使用:

 public Task<HttpResponseMessage> Post( Par one , Par two )
        {
...
...
...

在我的机器人方面:

  URL url = new URL("myurl/par1/par2/"); //  URL url = new URL("myurl") this works without parameters

            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
...
...
...
...

但是当我使用它时,我从服务器得到 404 响应。

我该如何解决这个问题?

提前致谢

1 个答案:

答案 0 :(得分:0)

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
    nameValuePair.add(new BasicNameValuePair("param1", "param1"));
    nameValuePair.add(new BasicNameValuePair("param1", "param1"));

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
       HttpResponse response = httpClient.execute(httpPost);

了解here如何将HttpsURLConnectionList<NameValuePair>

一起使用

在你的情况下;你需要用带有这样功能的params写url。

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

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

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

    return result.toString();
}

然后提出请求。

conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

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

    conn.connect();