0

I have a method which can be upload photo like this :

 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;
        }

    }

It works fine. But I want to send parameters to that method, How can I do that?

Should I use like this:

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

On my android side :

  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
...
...
...
...

But when I use this , I got 404 response from server.

How can I solve this issue?

thanks in advance

CompEng
  • 7,161
  • 16
  • 68
  • 122

1 Answers1

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);

Take alook at here how to use HttpsURLConnection with the List<NameValuePair>

in your case;U need to write url with params with a function like this.

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

and later make a request.

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();
Community
  • 1
  • 1
sakir
  • 3,391
  • 8
  • 34
  • 50
  • thanks for answer , so In MVC side , Should I use public Task Post( Par one , Par two ) { .... – CompEng Dec 15 '14 at 12:24
  • I didnt know your project detail but ,u can try to get the params like this (HttpRequestMessage request) check the "request" value from server side – sakir Dec 15 '14 at 12:30