根据URL请求 返回XML字符串
2014-05-13 12:00
363 查看
public static string GetHttpResponse(string url)
{
string content = "";
// Create a new HttpWebRequest object.Make sure that
// a default proxy is set if you are behind a fure wall.
//其中,HttpWebRequest实例不使用HttpWebRequest的构造函数来创建,二是使用WebRequest的Create方法来创建.
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(url);
//myHttpWebRequest1.Method = "POST";
//myHttpWebRequest1.ContentType = "application/x-www-form-urlencoded";
////不维持与服务器的请求状态
myHttpWebRequest1.KeepAlive = true;
//myHttpWebRequest1.AllowAutoRedirect = false;
//创建一个HttpWebRequest对象
//Assign the response object of HttpWebRequest to a HttpWebResponse variable.\
HttpWebResponse myHttpWebResponse1;
try
{
myHttpWebResponse1 = (HttpWebResponse)myHttpWebRequest1.GetResponse();
//设置页面的编码模式
//System.Text.Encoding utf8 = System.Text.Encoding.gb;
//System.Text.Encoding.GetEncoding("GB2312").GetString(System.Text.Encoding.UTF8.GetBytes("你的xml字符串"));
Stream streamResponse = myHttpWebResponse1.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse, System.Text.Encoding.GetEncoding("GB2312"));
Char[] readBuff = new Char[256];
//这里使用了StreamReader的Read()方法,参数意指从0开始读取256个char到readByff中.
//Read()方法返回值为指定的字符串数组,当达到文件或流的末尾使,方法返回0
int count = streamRead.Read(readBuff, 0, 256);
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
content += outputData;
count = streamRead.Read(readBuff, 0, 256);
}
myHttpWebResponse1.Close();
return content;
}
catch (WebException ex)
{
content = "在请求URL为:" + url.ToString() + " 的页面时产生错误,错误信息为" + ex.ToString();
return content;
}
}
public static IdentlyInfo CredentialsInfo(string identity, string userName)
{
IdentlyInfo identlyInfo = new IdentlyInfo();
string retStr = "";
string url = "http://172.168.254.34:9090/NCIIS/PersonInfoServlet?id=" + identity + "&name=" + userName + "";
// strResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><status></status><id>身份证号码</id><name>姓名</name><photo>http://b.hiphotos.baidu.com/image/pic/item/f11f3a292df5e0fef825890b5e6034a85edf72fe.jpg</photo></data>";
string strResult = GetHttpResponse(url);
int start, end;
string statusCode = "<status>";
// string strorderstate = null;
start = strResult.LastIndexOf(statusCode) + statusCode.Length;
int starts = strResult.IndexOf("</status>");
if (start != -1)
{
identlyInfo.status = strResult.Substring(start, starts - start);
}
string id = "<id>";
//string strid = null;
start = strResult.LastIndexOf(id) + id.Length;
starts = strResult.IndexOf("</id>");
if (start != -1)
{
identlyInfo.id = strResult.Substring(start, starts - start);
}
string name = "<name>";
//string strname = null;
start = strResult.LastIndexOf(name) + name.Length;
starts = strResult.IndexOf("</name>");
if (start != -1)
{
identlyInfo.name = strResult.Substring(start, starts - start);
}
string photo = "<photo>";
//string strphoto = null;
start = strResult.LastIndexOf(photo) + photo.Length;
starts = strResult.IndexOf("</photo>");
if (start != -1)
{
identlyInfo.photo = strResult.Substring(start, starts - start);
}
if (identlyInfo.status != "00")
{
retStr = "身份证和姓名不匹配!";
}
else
{
retStr = identlyInfo.photo;
}
return identlyInfo;
}
{
string content = "";
// Create a new HttpWebRequest object.Make sure that
// a default proxy is set if you are behind a fure wall.
//其中,HttpWebRequest实例不使用HttpWebRequest的构造函数来创建,二是使用WebRequest的Create方法来创建.
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(url);
//myHttpWebRequest1.Method = "POST";
//myHttpWebRequest1.ContentType = "application/x-www-form-urlencoded";
////不维持与服务器的请求状态
myHttpWebRequest1.KeepAlive = true;
//myHttpWebRequest1.AllowAutoRedirect = false;
//创建一个HttpWebRequest对象
//Assign the response object of HttpWebRequest to a HttpWebResponse variable.\
HttpWebResponse myHttpWebResponse1;
try
{
myHttpWebResponse1 = (HttpWebResponse)myHttpWebRequest1.GetResponse();
//设置页面的编码模式
//System.Text.Encoding utf8 = System.Text.Encoding.gb;
//System.Text.Encoding.GetEncoding("GB2312").GetString(System.Text.Encoding.UTF8.GetBytes("你的xml字符串"));
Stream streamResponse = myHttpWebResponse1.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse, System.Text.Encoding.GetEncoding("GB2312"));
Char[] readBuff = new Char[256];
//这里使用了StreamReader的Read()方法,参数意指从0开始读取256个char到readByff中.
//Read()方法返回值为指定的字符串数组,当达到文件或流的末尾使,方法返回0
int count = streamRead.Read(readBuff, 0, 256);
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
content += outputData;
count = streamRead.Read(readBuff, 0, 256);
}
myHttpWebResponse1.Close();
return content;
}
catch (WebException ex)
{
content = "在请求URL为:" + url.ToString() + " 的页面时产生错误,错误信息为" + ex.ToString();
return content;
}
}
public static IdentlyInfo CredentialsInfo(string identity, string userName)
{
IdentlyInfo identlyInfo = new IdentlyInfo();
string retStr = "";
string url = "http://172.168.254.34:9090/NCIIS/PersonInfoServlet?id=" + identity + "&name=" + userName + "";
// strResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><status></status><id>身份证号码</id><name>姓名</name><photo>http://b.hiphotos.baidu.com/image/pic/item/f11f3a292df5e0fef825890b5e6034a85edf72fe.jpg</photo></data>";
string strResult = GetHttpResponse(url);
int start, end;
string statusCode = "<status>";
// string strorderstate = null;
start = strResult.LastIndexOf(statusCode) + statusCode.Length;
int starts = strResult.IndexOf("</status>");
if (start != -1)
{
identlyInfo.status = strResult.Substring(start, starts - start);
}
string id = "<id>";
//string strid = null;
start = strResult.LastIndexOf(id) + id.Length;
starts = strResult.IndexOf("</id>");
if (start != -1)
{
identlyInfo.id = strResult.Substring(start, starts - start);
}
string name = "<name>";
//string strname = null;
start = strResult.LastIndexOf(name) + name.Length;
starts = strResult.IndexOf("</name>");
if (start != -1)
{
identlyInfo.name = strResult.Substring(start, starts - start);
}
string photo = "<photo>";
//string strphoto = null;
start = strResult.LastIndexOf(photo) + photo.Length;
starts = strResult.IndexOf("</photo>");
if (start != -1)
{
identlyInfo.photo = strResult.Substring(start, starts - start);
}
if (identlyInfo.status != "00")
{
retStr = "身份证和姓名不匹配!";
}
else
{
retStr = identlyInfo.photo;
}
return identlyInfo;
}
相关文章推荐
- 根据传入url请求,返回json字符串
- 根据传入url请求,返回json字符串
- 根据对方提供的URL获取返回的数据流并保存为XML
- 根据对方提供的URL获取返回的数据流并保存为XML
- 根据对方提供的URL获取返回的数据流并保存为XML
- 淘宝开放平台是通过url请求获取结果xml字符串
- springmvc 视图解析器 原理 返回根据请求返回xml或者json
- 用一个WEB服务或普通站点 实现这样一个效果?以URL请求,返回一个XML文档
- Spring MVC Controller 的同一个 URL 请求,根据逻辑判断返回 JSON 或者 HTML 视图
- 根据对方提供的URL获取返回的数据流并保存为XML
- HttpURLConnection处理post请求,返回字符串
- 获取远程的字符串(请求一个URL, 返回这个请求页面的页面数据)
- dom4j来解析xml,通过URL获取服务器端返回的字符串,java swing 实现的页面
- 如何让 Spring MVC Controller 的同一个 URL 请求,根据逻辑判断返回 JSON 或者 HTML 视图?
- 请求url并返回信息以xml形式显示
- HttpURLConnection处理post请求,返回字符串
- 向指定 URL 发送GET,POST方法的请求(传入指定url和参数,服务器返回字符串数据)
- 如何让 Spring MVC Controller 的同一个 URL 请求,根据逻辑判断返回 JSON 或者 HTML 视图?
- python 读取文件里的每行url 返回每个url请求响应页面的title!
- 访问url返回xml进行解析存入数据库