将以下Java代码转换为C# [英] To convert the following java code to c#

查看:79
本文介绍了将以下Java代码转换为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,请帮助我将以下代码转换为c#

hi pl help me in converting the following code to c#

HttpPost httpPost = new HttpPost("http://www.inspire-geoportal.eu/INSPIREValidatorService/resources/validation/inspire");
//xml response: httpPost.addHeader("Accept", "application/xml");
//html response
httpPost.addHeader("Accept", "text/html");
FileBody dataFile = new FileBody(new File("yourMetadataFile.xml"));
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("dataFile", dataFile);
httpPost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
    //OK
    case 200:   //implement the below method to extract the response
                parseServiceResponse(response);
                break;
    //Exception was thrown
    case 400:   //implement the below method to handle the exceptions
                handleServiceException(response);
                break;
    //Internal error from the service
    default:    //implement the below method to handle errors such as internal server error
                handleServerError(response);

推荐答案

如果要在C#上请求"POST"方法,则应使用:
If you want to request a "POST" method on C#.You should use :
string data; // SENDING DATA
           byte[] buffer = Encoding.UTF8.GetBytes(data);
           HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://www.inspire-geoportal.eu/INSPIREValidatorService/resources/validation/inspire");
           hwr.Method = "POST";
           hwr.ContentType = "application/x-www-form-urlencoded";
           hwr.ContentLength = buffer.Length;
           Stream PostStream = hwr.GetRequestStream();
           PostStream.Write(buffer, 0, buffer.Length);
           PostStream.Close();



但是我不知道文件代码部分.



But i dont know about file code part.


这篇关于将以下Java代码转换为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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