Android发布请求aspx

时间:2012-01-28 07:04:22

标签: android web-services

我是Android的新手,我有发布请求的C#代码,我想在android中执行相同的操作。  你能帮助我吗?这是C#代码片段。

WebRequest request = WebRequest.Create("http://domani.url.com");

            // Set the Method property of the request to POST.
            request.Method = "POST";

            // Create POST data and convert it to a byte array.
            string postData = "©req:1©un:username©pwd:password©flag:MA©ver:1.9.20©";

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";

            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;

            // Get the request stream.
            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }

            // Get the response.
            using (WebResponse response = request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);

                // Get the stream containing content returned by the server.
                 string responseFromServer = reader.ReadToEnd();

                // Display the content.
                 Response.Write(responseFromServer);
            }

1 个答案:

答案 0 :(得分:1)

public class HttpMultiPartPost
{
    String filePath;
    String sig_key;
    HttpPost httpPost;
    String aa ;



    String response;
    InputStream responseStream;

    public HttpMultiPartPost()
    {
    }

    public HttpMultiPartPost(String filePath, String sig_key) 
    {
        this.filePath = filePath;
        this.sig_key = sig_key;
    }

    public void httpPostRequestMultipart() 
    {
        try 
        {
        //  Uri.Builder builder = new Uri.Builder();
        //  builder.scheme("http");
        //  builder.authority("api.flickr.com");
        //  builder.path("services/upload/");

            httpPost = new HttpPost("http://46.105.8.59/?imprimer");

            MultipartEntity entity = new MultipartEntity();

            //entity.addPart("subject", new StringBody("subject"));
            //entity.addPart("message", new StringBody("Prashant"));
        //  entity.addPart("photo", new FileBody(new File(filePath)));
            entity.addPart("bdata", new StringBody("radiateurs_elec"));
            entity.addPart("ref_lm", new StringBody("65249632"));
            //entity.addPart("email", new StringBody("ajayraikwar03@gmail.com"));
            //entity.addPart("submit", null);

            httpPost.setEntity(entity);

            DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
            HttpResponse httpResponse;

            try
            {
                httpResponse = defaultHttpClient.execute(httpPost);


                Log.d("Prashant :", EntityUtils.toString(httpResponse.getEntity()) + "");

//              if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
//              {
//                  
//                  // EntityUtils.toString(httpResponse.getEntity());
//                   
//               aa = EntityUtils.toString(httpResponse.getEntity());   
//               
//              //  responseStream = httpResponse.getEntity().getContent();
//                  
//              //  Log.d("Resp :", responseStream + "");
//                  
//                  Log.d("Response :", aa + "");
//                  
//              }
            } catch (ClientProtocolException e) 
            {
                e.printStackTrace();
            }

            Log.d("Resp :", responseStream + "");

        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }


    public void setResponseStream(InputStream responseStream) 
    {
        this.responseStream = responseStream;
    }
    public InputStream getResponseStream() 
    {
        return responseStream;
    }







}