根据URL请求 返回XML字符串
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;
}
根据URL请求 返回XML字符串的更多相关文章
- ajax请求返回json字符串/json对象 处理
1. 返回json字符串如何处理 $.ajax({ url:xxx, success:function(date){ }, error:function(){ } }); 通过最原始的返回: Prin ...
- 【Spring MVC】spring mvc中相同的url请求返回不同的结果
在项目中凡是使用Spring MVC这种控制器的,大多都是返回JSON数据对象,或者JSP页面. 但是相同的URL请求如何让他自动的选择放回的是什么? 在这里有由于鄙人没有亲自测试过,就不敢乱贴代码, ...
- c# 对SOAP返回XML字符串的解析方法
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP:Head ...
- jsp Ajax请求(返回xml数据类型)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- ajax请求返回Json字符串运用highcharts数据图表展现数据
[1].[图片] Json字符串和highcharts数据图表展现.jpg 跳至 [1] code=26754#44745" rel="nofollow"> [2] ...
- .net 超长URL请求返回404错误-解决方法
<system.webServer> <security> <requestFiltering> <requestLimits maxQueryString= ...
- url请求返回结果测试工具(CURL)
官网:http://curl.haxx.se/download.html 具体用法用时百度 或 到时再补充
- springmvc返回json字符串中文乱码问题
问题: 后台代码如下: @RequestMapping("menuTreeAjax") @ResponseBody /** * 根据parentMenuId获取菜单的树结构 * @ ...
- Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据
我们都知道Asp.net MVC自带的Action可以有多种类型,比如ActionResult,ContentResult,JsonResult……,但是很遗憾没有支持直接返回XML的XmlResul ...
随机推荐
- window安装rabbitmq
Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接可以下载安装最新的版本: 下载并安装Eralng OTP For Wi ...
- 在modelsim中加入quartus仿真库
找到modelsim安装目录下的modelsim.ini文件. 将modelsim.ini的只读属性去掉. 打开quartus软件.选择Launch Simulation Library Compil ...
- C# 操作超时正常还是错
net(客户端)调用IIS(服务端)出现503后,就报操作超时错误 问题描述:服务端环境:IIS 客户端环境:windowsxp + iis + .net 调用时出现如下错误:System.Net.W ...
- iOS_16_控制器切换_modal_storyboard拖线的方式
终于效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill ...
- 推荐vim配置
"设置编码,处理中文乱码,文件默认utf8编码set fileencodings=utf-8,ucs-bom,cp936,big5 "设置默认配色方案colorscheme def ...
- 我的 Android 开发实战经验总结
我的 Android 开发实战经验总结 字数4440 阅读5137 评论43 喜欢250 曾经一直想写一篇总结 Android 开发经验的文章,预计当时的我还达不到某种水平,所以思路跟不上,下笔又捉襟 ...
- constructors and destructors
A constructor is a method that gets called immediately when an object is allocated (on the stack or ...
- python之斐波那契数列
斐波那契数列(Fibonacci sequence),又称黄金分割数 这样的一个数列:0,1,1,2,3,8,13,21,34…….. 特别指出:0不是第一项,而是第零项. 在数学上被以 递归的方法定 ...
- Matlab字符串分割
data = '1.21, 1.985, 1.955, 2.015, 1.885'; C = strsplit(data,', ') C = '1.21' '1.985' '1.955' '2.015 ...
- python 面试题 string int
str1 = 'hello' str2 = str1 str3 = str1 str4 = str1 str1 = '' int1 = 1 int2 = int1 int3 = int1 int4 = ...