如何将下面2个java的webhttp代码转换成c#代码?

vtyvpn 2016-04-11 08:02:16
String login(String IPADDR, String pwd, String usrname)
throws URISyntaxException, ClientProtocolException, IOException {
HttpUriRequest lg= RequestBuilder.post()
.setUri(new URI("http://" + IPADDR+ "/main/certificate"))
.addParameter("password", pwd)
.addParameter("username", usrname).build();
CloseableHttpResponse rspne = httpclient.execute(lg);
try {
HttpEntity entity = rspne .getEntity();
String str = EntityUtils.toString(entity);
return str ;
} finally {
rspne .close();
}
}


String addUser(String IPADDR, String username, String pwd)
throws URISyntaxException, ClientProtocolException, IOException {
HttpPost htpPost=new HttpPost("http://" + IPADDR+ "/main/usr/add");
List <BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", pwd));
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));

CloseableHttpResponse respnse = httpclient.execute(htpPost);

try {
HttpEntity entity = respnse .getEntity();
String str = EntityUtils.toString(entity);
return str ;
} finally {
respnse .close();
}
}
...全文
107 1 打赏 收藏 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
licitykity 2016-04-11
  • 打赏
  • 举报
回复
StringBuilder sb = new StringBuilder(); sb .append("username= " +"admin" ); sb.append("password=" +"123456"); string url=@"http://" + ipaddr + "/...."; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; Encoding myEncoding = Encoding.UTF8; request.Method = "POST"; request.KeepAlive = false; request.AllowAutoRedirect = true; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"; request.ContentLength = postData.Length; System.IO.Stream outputStream = request.GetRequestStream(); outputStream.Write(postData, 0, postData.Length); outputStream.Close(); HttpWebResponse response; Stream responseStream; StreamReader reader; string srcString; response = request.GetResponse() as HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8")); srcString = reader.ReadToEnd(); string result = srcString; reader.Close(); return result;

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧